Android 不需要打开应用程序 activity 而是打开其他应用程序
Android app doesn't open required activity instead it opens other
帮助我解决我一周以来一直面临的问题
我制作了一个 android 应用程序,其中有 3 个名为
的活动
MainActivity
NotificationListActivity
NotificationActivity
除此之外,我的应用程序中还有 2 个服务(因为我使用的是 FCM)
JLZFirebaseMsgService
JLZInstanceIdService
在JLZFirebaseMsgService.java我写了代码
private void sendNotification(String messageBody) {
Intent intent = new Intent(this,NotificationActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Jee Lo Zindagi 2.0")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
我想要的是每当 FCM 服务器向设备发送通知时,应用程序应该按照我写的那样打开 NotificationActivity
但是每当我点击通知时它都会打开 MainActivity 我不知道为什么。
当应用程序打开时,通知会出现,通过点击它,我们会转到我想要的 NotificationActivity,但当应用程序关闭时,情况就不一样了。
但有时应用程序会通过点击通知显示正确的 activity 我仍然不知道为什么。
您应该在服务器代码中使用 "data" 而不是 "notification"
当您在服务器代码上使用 "notification" 时
当您的应用程序不是 运行 时,JLZFirebaseMsgService.java 不会启动。相反,您将收到带有启动器图标的通知
但是,如果您在服务器代码上使用 "data",您的消息服务将启动
您似乎在发送通知消息,这些消息在您的应用程序处于前台时会转到 onMessageReceived,在您的情况下,这将导致您手动生成通知,而当您的应用程序在后台时,它将导致自动生成您的代码未定义的通知。
如果您使用的是 FCM REST API,那么您可以使用 click_action 字段,它允许您定义当用户点击通知时将启动哪个 activity .如果您目前正在使用 Firebase 控制台,则无法从那里指定 click_action。
帮助我解决我一周以来一直面临的问题 我制作了一个 android 应用程序,其中有 3 个名为
的活动MainActivity
NotificationListActivity
NotificationActivity
除此之外,我的应用程序中还有 2 个服务(因为我使用的是 FCM)
JLZFirebaseMsgService
JLZInstanceIdService
在JLZFirebaseMsgService.java我写了代码
private void sendNotification(String messageBody) {
Intent intent = new Intent(this,NotificationActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Jee Lo Zindagi 2.0")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
我想要的是每当 FCM 服务器向设备发送通知时,应用程序应该按照我写的那样打开 NotificationActivity
但是每当我点击通知时它都会打开 MainActivity 我不知道为什么。
当应用程序打开时,通知会出现,通过点击它,我们会转到我想要的 NotificationActivity,但当应用程序关闭时,情况就不一样了。
但有时应用程序会通过点击通知显示正确的 activity 我仍然不知道为什么。
您应该在服务器代码中使用 "data" 而不是 "notification" 当您在服务器代码上使用 "notification" 时 当您的应用程序不是 运行 时,JLZFirebaseMsgService.java 不会启动。相反,您将收到带有启动器图标的通知 但是,如果您在服务器代码上使用 "data",您的消息服务将启动
您似乎在发送通知消息,这些消息在您的应用程序处于前台时会转到 onMessageReceived,在您的情况下,这将导致您手动生成通知,而当您的应用程序在后台时,它将导致自动生成您的代码未定义的通知。
如果您使用的是 FCM REST API,那么您可以使用 click_action 字段,它允许您定义当用户点击通知时将启动哪个 activity .如果您目前正在使用 Firebase 控制台,则无法从那里指定 click_action。