简体   繁体   English

如何在特定活动中禁用 FCM Android

[英]How to disable FCM on spesific activity Android

i want to ask about FCM.我想问一下FCM。 I have an FCM service that is running as it should this is my code我有一个正在运行的 FCM 服务,这是我的代码

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "ch1")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        NotificationChannel channel = new NotificationChannel("ch1",
                getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
        channel.enableLights(true);
        channel.enableVibration(true);
        mNotificationManager.createNotificationChannel(channel);
    }
    mNotificationManager.notify(0, notificationBuilder.build());

but i want to disable this notification on spesific activity, for example i have ChatActivity.但我想在特定活动上禁用此通知,例如我有 ChatActivity。 where if i get notificatin on this activity, the fcm service is not showing a notification.如果我收到有关此活动的通知,fcm 服务不会显示通知。 I have tried implement this code but still not work我已经尝试实现此代码但仍然无法正常工作

if(!(this.getApplicationContext() instanceof ChatActivity)){
   //build the notification
}

anyone have solution for my problem?有人能解决我的问题吗? thank you.谢谢你。

First of all keep a flag in your SharedPreferences for ChatActivity to know if it's open or not.首先,在您的SharedPreferences中为ChatActivity保留一个标志,以了解它是否打开。

Now inside ChatActivity set this flag true in onResume() and false in onStop() .现在在ChatActivity中,在onResume()中将此标志设置为true ,在onStop()中设置为false Like this:像这样:

@Override
protected void onResume() {
    super.onResume();
    AppPreferences.getInstance().setBoolean("IS_CHAT_ACTIVITY", true);
}

@Override
protected void onStop() {
    super.onStop();
    AppPreferences.getInstance().setBoolean("IS_CHAT_ACTIVITY", true);
}

If you don't know anything about SharedPreferences check This Tutorial .如果您对SharedPreferences一无所知,请查看本教程

Now in your FCM Service class, you have onMessageReceived() callback, which is triggered every time a notification comes.现在在您的FCM Service class 中,您有onMessageReceived()回调,每次收到通知时都会触发该回调。

Now Do this in your onMessageReceived() callback:现在在您的onMessageReceived()回调中执行此操作:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if(remoteMessage.getNotification() != null) {
        if (AppPreferences.getInstance().getBoolean("IS_CHAT_ACTIVITY", false)) {
            //Do Nothing, Ignore Notification
        } else {
            //Write code to show notification here
            //Show Notification
            generateNotification(remoteMessage.getNotification());
        }
    }
}

That's it, that's how you can prevent the notification while you're on ChatActivity .就是这样,这就是您在使用ChatActivity时阻止通知的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM