![](/img/trans.png)
[英]How can I disable receiving notification in a specific activity in kotlin?
[英]How to disable all the incoming push notification when your in the desired activity?
正如标题所暗示的那样,当我处于所需的活动中时,如何防止我的应用程序收到通知? 我正在开发一个聊天应用程序,其中用户可以在发布新消息时收到通知,当用户处于聊天活动中时如何阻止通知?
这是 FirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
super.onMessageReceived(message);
int requestID = (int) System.currentTimeMillis();
String title = message.getNotification().getTitle();
String body = message.getNotification().getBody();
String click_action=message.getNotification().getClickAction();
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),"Notification");
builder.setContentTitle(title);
builder.setContentText(body);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
builder.setLights(getResources().getColor(R.color.chitchat), 3000, 3000);
builder.setSmallIcon(R.drawable.logowhite);
Intent intent = null;
//message.getData().get("type");
if (Objects.requireNonNull(message.getData().get("type")).equalsIgnoreCase("privatechat"))
{
intent = new Intent(click_action);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("GCKey", message.getData().get("GCKey"));
intent.putExtra("GCNameKey", message.getData().get("GCNameKey"));
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE );
builder.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("Notification", "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(69, builder.build());
}
}
这应该不难。 您可以只创建一个 class,其中您有一个 static 变量,您只需在其中设置活动并在通知之前检查是否要显示通知。 这将 go 这样:
使用 static 变量创建一个新的 class
public class NotificationHelper { public static boolean shouldShowNotification = true; }
在您不希望通知显示的活动中添加此代码:
@Override public void onResume(){ super.onResume(); NotificationHelper.shouldShowNotification = false; } @Override public void onPause(){ super.onPause(); NotificationHelper.shouldShowNotification = true; }
在MyFirebaseMessagingService
class 中,在执行代码之前添加一个条件。
public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(@NonNull RemoteMessage message) { super.onMessageReceived(message); if(NotificationHelper.shouldShowNotification){ // your code goes here... } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.