当用户点击 Firebase Cloud Messaging (FCM) 创建的通知时,我尝试打开NotificationActivity。但是,应用程序始终启动 MainActivity,而不是所需的 NotificationActivity。
当我通过应用程序中的按钮单击创建通知时,它会正确打开NotificationActivity。但是,当 FCM(通过 MyFirebaseMessagingService)生成通知时,点击通知会打开 MainActivity。
以下是我的代码和配置的相关部分:
1。 Firebase 消息服务
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
showNotification(
getApplicationContext(),
remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody()
);
}
}
public static void showNotification(Context context, String title, String message) {
String channelId = "general";
Intent intent = new Intent(context, NotificationActivity.class);
intent.setAction("OPEN_NOTIFICATION_ACTIVITY");
intent.putExtra("notification_title", title);
intent.putExtra("notification_body", message);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
channelId, "General", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(1, mBuilder.build());
}
}
2。这是我的 AndroidManifest.xml 配置:
<service
android:name=".service.MyFirebaseMessagingService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name=".NotificationActivity"
android:exported="true">
<intent-filter>
<action android:name="OPEN_NOTIFICATION_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
3.观察结果
预期行为:点击通知时,NotificationActivity 应打开,显示 Intent 中传递的标题和正文。
实际行为: MainActivity 打开而不是NotificationActivity。
问题是,当您的应用程序处于后台或关闭时,Firebase 会自动创建通知,并且根本不会调用
onMessageReceived
。正如您所说,通知在通过按钮单击显示时打开所需的活动,此时,通知是由您创建的,并且您专门编写了打开NotificationActivity
。现在,当 Firebase 创建通知时,它仅打开 MainActivity
。
将
click_action
与 Firebase 消息一起传递,如下所示:
{
"to": "some_device_token",
"notification": {
"title": "hello",
"body": "yo",
"click_action": "OPEN_NOTIFICATION_ACTIVITY" // for intent filter in your activity
},
"data": {
"extra": "extra data here..."
}
}
希望这有帮助。