【问题标题】:How to disable showing notification when it it comes to the system tray?当涉及到系统托盘时,如何禁用显示通知?
【发布时间】:2016-10-24 07:23:02
【问题描述】:

大家好,我正在开发一个使用 Firebase 云消息传递的应用程序。但是我有一种情况,我不想让用户看到何时收到带有数据消息的通知。我已经通过从 myFirebaseMessagingService 中删除函数 sendNotification 解决了这个问题,但这仅在我的应用程序处于前台时才有效。我的问题是:当应用程序在后台并且通知来到系统托盘时,如何设置代码使通知图标不会显示?

这是我的 MainActivity:

public class MainActivity extends AppCompatActivity {
Button dugme, dugme2, dugmeBaza, dugmeToken;
DataBaseHelper db;
Cursor c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("onCreate", "ONCREATE");
    db=new DataBaseHelper(this);
    final Intent intent=getIntent();
    setContentView(R.layout.activity_main);
    String msg = getIntent().getStringExtra("click_action");
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if (msg != null)
    {
        Log.d("MSG", msg);
        if (msg.equals("goToFragment1")) {
            Fragment1 fragment1 = new Fragment1();
            fragmentTransaction.replace(R.id.myFragment, fragment1);
            Log.d("FragmentTransaction", "Fragment je promenjen u onCreate!");
            fragmentTransaction.commit();
            Log.d("Create", "Kraj onCreatea");

        }
    }

    dugme = (Button) findViewById(R.id.dugme1);
    dugme2 = (Button) findViewById(R.id.subscribe);
    dugme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Fragment fragment = null;
            if (view == dugme) {
                fragment = new Fragment1();
            }
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.myFragment, fragment);
            transaction.addToBackStack(null);
            transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
            transaction.commit();
        }
    });


    dugme2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FirebaseMessaging.getInstance().subscribeToTopic("android");
            Log.d("Log", "Uspesno ste se pretplatili");
        }
    });
    dugmeBaza=(Button)findViewById(R.id.dugmeZabazu);
    viewAll();
    dugmeToken=(Button)findViewById(R.id.TokenButton);
    dugmeToken.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            Log.d("TOKEN", "Refreshed token: " + refreshedToken);
        }
    });


@Override
protected void onPause() {
    super.onPause();  // Always call the superclass method first

    Log.d("onPause", "Pauza");
}
public void viewAll(){

    dugmeBaza.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Cursor res= db.getAlldata();
            if(res.getCount()==0) {
                //show message
                showMessage("Error", "Nothing found");


                return;

            }
            StringBuffer buffer=new StringBuffer();
            while (res.moveToNext()){
                buffer.append("Id: " + res.getString(0) + "\n");
                buffer.append("poruka: " + res.getString(1));

            }
            showMessage("Data", buffer.toString());
        }
    });
}
public void showMessage(String title, String message){
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}
public void Ubaci(){
    String msg=getIntent().getStringExtra("poruka");
    db.insertMsg(msg);
}

这里是 myFirebaseMessagingService:

public class myFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG="MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("onMessageReceived", "Pozvana funkcija onMessageReceived");
    Log.d(TAG, "From " + remoteMessage.getFrom());
    Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
    Log.d(TAG, "Location " + remoteMessage.getNotification().getClickAction());
    Log.d(TAG, "Value " + remoteMessage.getData().get("click_action"));

【问题讨论】:

    标签: android firebase-notifications background-application


    【解决方案1】:

    这实际上是由推送通知的发送者控制的。根据current documentation

    通知: GCM 会代表客户端应用自动向最终用户设备显示消息。 通知具有预定义的 一组用户可见的键。设置通知负载。可能有可选 数据有效载荷。始终可折叠。

    数据: 客户端应用负责处理数据消息。数据消息只有自定义键/值对。仅设置数据有效负载。能 可折叠或不可折叠。

    若要在应用程序处于后台时禁用通知的自动显示,请将发送方不发送负载的“通知”部分并在负载的“数据”部分发送所有内容。这样,应用程序代码将始终处理传入消息。然后它可以选择是否显示通知。

    【讨论】:

    • 所以,如果我理解正确,发件人(在我的情况下是应用服务器)决定是否显示通知图标,而不是我在 Android 上(我的任务是在 android 设备上为 FCM 提供连接)?
    • 目前就是这样。这是他们提供给发件人服务器的附加功能。您可以让服务器只发送“数据”,然后您可以在应用中决定是否显示通知。
    • 好的,但是如果我想在服务器只发送数据时显示通知,我怎么能在应用程序中决定呢?
    • "data" 对象内部可以包含任何形式的数据。例如data.eventType:NEW_COMMENTdata.from:user02 等。应用端代码可以决定在哪些事件上显示通知、显示哪些文本以及单击时打开哪个 Activity。
    • 想指出,SD 在他的回答中肯定引用了相关文档,但它们是 GCM 文档——不是 FCM 文档,您可以在 firebase.google.com/docs/cloud-messaging/… 找到。信息几乎相同,因此有点令人困惑。只要 GCM 作为 GCM 仍被支持,这种情况就会存在。
    【解决方案2】:

    您可以通过重写 handleIntent 方法并在 FirebaseMessagingService 中注释/删除 super.handleIntent(intent) 来实现此目的;

    如果应用程序在后台并且负载中存在通知标签,则基本上会调用它。

    扩展 FirebaseMessagingService 类,您将拥有

    @Override public void handleIntent(Intent intent) { // your code should be here }

    一旦它来到这里,您就可以自定义您自己的 NoificationManager

    此方法只能在 fcm 11.0.6 之前被覆盖

    【讨论】:

    • 您能否分享一些示例代码以帮助我更好地理解?
    • @kilokahn 请查看编辑后的答案并向我解释拒绝投票的原因。
    • 您的回答在编辑前过于含糊 - 答案缺少资源链接或说明建议的方法如何有用。编辑后,答案仍然无效。如果您无法获取 OP 提供的代码并演示您的答案建议的外观,一些伪代码会有所帮助。
    【解决方案3】:

    我禁用了系统托盘中的通知。我改变了消息的结构。

    我使用 python 在服务器端发送通知。所以我通过data_message(这是python中的字典)更改了message_body,这就是我所做的全部更改,它工作正常!!

    【讨论】:

    • 你能详细解释一下我如何存档
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多