开发聊天应用:如何在指定Activity中禁用推送通知?
解决聊天页面屏蔽推送通知的问题
要实现用户处于聊天Activity时不弹出通知,核心思路是跟踪聊天页面的活跃状态,在收到推送消息时判断当前状态,决定是否显示通知。下面提供两种实用实现方式:
方式一:简单判断聊天页面是否活跃
适合单聊天窗口场景,只要用户在聊天页面,就屏蔽所有聊天类通知。
1. 修改聊天Activity,添加状态跟踪
在你的ChatActivity中添加静态变量,记录页面是否处于前台:
public class ChatActivity extends AppCompatActivity { // 标记聊天页面是否活跃 public static boolean isChatActive = false; @Override protected void onResume() { super.onResume(); // 页面进入前台,标记为活跃 isChatActive = true; } @Override protected void onPause() { super.onPause(); // 页面退到后台,标记为非活跃 isChatActive = false; } }
2. 在Firebase消息服务中判断状态
修改MyFirebaseMessagingService的onMessageReceived方法,在构建通知前加入状态判断:
@Override public void onMessageReceived(@NonNull RemoteMessage message) { super.onMessageReceived(message); // 若聊天页面处于活跃状态,直接返回,不显示通知 if (ChatActivity.isChatActive) { return; } // 以下是原有的通知构建逻辑 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; if (Objects.requireNonNull(message.getData().get("type")).equalsIgnoreCase("privatechat")) { intent = new Intent(click_action); 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")); } if (intent != null) { 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()); }
方式二:精准判断当前聊天会话
如果应用支持多聊天窗口,仅屏蔽当前正在聊天的会话的通知,其他会话通知正常显示,可采用这种方式:
1. 修改聊天Activity,跟踪当前会话ID
public class ChatActivity extends AppCompatActivity { public static boolean isChatActive = false; // 记录当前聊天的会话ID public static String currentChatGCKey = null; @Override protected void onResume() { super.onResume(); isChatActive = true; // 从Intent中获取当前聊天的GCKey currentChatGCKey = getIntent().getStringExtra("GCKey"); } @Override protected void onPause() { super.onPause(); isChatActive = false; currentChatGCKey = null; } }
2. 在消息服务中对比会话ID
修改onMessageReceived中的判断逻辑:
@Override public void onMessageReceived(@NonNull RemoteMessage message) { super.onMessageReceived(message); // 获取当前消息的会话ID String messageGCKey = message.getData().get("GCKey"); // 仅当用户在对应聊天页面时,才屏蔽通知 if (ChatActivity.isChatActive && ChatActivity.currentChatGCKey != null && ChatActivity.currentChatGCKey.equals(messageGCKey)) { return; } // 剩余通知构建逻辑和之前一致 // ... }
注意事项
- 静态变量在应用进程存活期间有效,若应用被后台回收,状态会自动重置,不影响正常逻辑。
- 原代码中重复设置Intent Flags,建议保留
FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP即可,避免清空栈影响用户当前操作。
内容的提问来源于stack exchange,提问作者CMRyle
相关产品推荐
相关产品推荐

