Pusher Beam通知点击处理问题:后台/终止状态无法跳转自定义Activity
解决后台/终止状态下Pusher Beam通知跳转自定义Activity的问题
核心原因
Firebase推送分两种类型,后台/终止状态下的行为存在差异:
- 通知消息(包含
notification字段):系统直接展示通知,不会触发onMessageReceived,点击默认打开应用启动页。 - 数据消息(仅含
data字段):无论应用处于前台、后台还是终止状态,都会触发onMessageReceived,由你自定义通知内容和跳转逻辑。
你的现有代码仅在前台生效,因为后台/终止时通知消息不会走onMessageReceived方法。
解决方案一:改用数据消息推送
让Pusher Beam只发送数据消息(去掉payload里的notification字段,所有内容放在data节点下)。这样无论应用处于什么状态,都会进入onMessageReceived,你现有的构建通知逻辑就能正常工作,点击通知会跳转到指定的MainActivity。
修改代码中的解析逻辑(从data中读取标题、内容等信息):
@Override public void onMessageReceived(RemoteMessage remoteMessage) { // 注释super调用,避免Pusher SDK默认处理干扰自定义逻辑 // super.onMessageReceived(remoteMessage); try { JSONObject json = new JSONObject(remoteMessage.getData().toString()); String title = json.getString("title"); String body = json.getString("body"); String clickAction = json.getString("click_action"); if ("com.pusher.pushnotifications.OPEN_TRACKING".equals(clickAction)) { Intent intent = new Intent(this, MainActivity.class); intent.putExtra("notification", "notification_center"); intent.setAction("notification_center"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE); // 保持原有通知通道创建逻辑 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(getString(R.string.channel_name), getString(R.string.description_channel_name), NotificationManager.IMPORTANCE_HIGH); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // 构建并发送通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.channel_name)) .setContentTitle(title) .setContentText(body) .setSmallIcon(R.mipmap.ic_launcher) .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(pendingIntent) .setAutoCancel(true); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(remoteMessage.getMessageId().hashCode(), builder.build()); } } catch (JSONException e) { e.printStackTrace(); } }
解决方案二:保留通知消息,配置点击行为
如果必须保留notification字段,需要配合以下步骤实现跳转:
- 在AndroidManifest.xml中配置目标Activity的Intent Filter
给MainActivity添加与推送click_action匹配的action过滤:
<activity android:name=".MainActivity"> <!-- 原有启动页Intent Filter --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- 添加处理通知点击的Filter --> <intent-filter> <action android:name="com.pusher.pushnotifications.OPEN_TRACKING" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
在推送Payload中设置click_action
确保Pusher Beam推送的notification字段里包含click_action,值为com.pusher.pushnotifications.OPEN_TRACKING。在目标Activity中处理跳转逻辑
后台点击通知启动Activity时,在MainActivity中读取Intent参数并处理:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handleNotificationIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); // 应用前台时,新Intent会走onNewIntent方法 handleNotificationIntent(intent); } private void handleNotificationIntent(Intent intent) { if (intent != null && "notification_center".equals(intent.getAction())) { String notificationType = intent.getStringExtra("notification"); // 这里编写跳转逻辑,比如打开通知中心页面 } }
注意事项
- 确保
PendingIntent的FLAG_MUTABLE适配Android 12+,你的代码已满足该要求。 - 方案二中,后台点击通知不会触发
onMessageReceived,系统会直接根据click_action启动Activity,因此参数需通过Intent传递并在Activity中处理。
内容的提问来源于stack exchange,提问作者sanjay
相关产品推荐
相关产品推荐

