应用关闭时的 Firebase 通知
Firebase notifications when app is closed
我在我的 Android 应用程序中实施了 Firebase 通知。当我的应用程序是 运行 时,通知以我的自定义布局显示,但当应用程序不是 运行 时,通知以默认布局显示。当应用程序不是 运行 时,如何将通知布局更改为我的布局。此外,我存储共享首选项以让用户切换通知。但是当应用程序不是 运行 时,无论如何都会显示通知。如何实现?
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(SettingsFragment.getReceiceNotification()){ //if user wants to receive notification
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.push_notification_layout);
remoteViews.setImageViewResource(R.id.push_notif_icon,R.mipmap.ic_bird_black);
Intent intent = new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContent(remoteViews);
notificationBuilder.setContentTitle("Radyo Türkkuşu");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
remoteViews.setTextViewText(R.id.push_title, "Radyo Türkkuşu");
remoteViews.setTextViewText(R.id.push_context, remoteMessage.getNotification().getBody());
//notificationBuilder.setLights (ContextCompat.getColor(MainActivity.context, R.color.pushColor), 5000, 5000);
notificationManager.notify(0,notificationBuilder.build());
}
}
你的问题是你将它与通知托盘一起使用。
看到这个link
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
如果您在应用程序处于后台时将 {data:"something"}(data-message)
与 {notification:"something"}(display-message)
一起使用,则数据负载将传递给 intent 的额外部分,但不会传递给 onMessageReceived()
method.I 假设你实现了显示通知的代码,所以当你的应用程序在前台时 onMessageReceived()
被触发并且它显示你想要的期望通知但是当它不是 onMessageReceived()
时没有获取触发器而不是 android 系统将使用您的通知负载处理它。您只需从服务器端代码中删除 {notification:"something"}(display-message/notification tray)
即可始终确保 onMessageReceived()
.
对于任何人保持提及 onMessageReceived()
将始终触发无论它是否是前景或背景请访问此 link
FCM 消息有两种类型
- 通知消息
- 数据消息
从 Firebase 控制台发送的消息是通知消息。为了在 onMessageReceived() 中获取消息,请使用 Data Message。在服务器端使用以下代码发送数据通知消息
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
引用https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
当您的应用关闭或 运行 在后台时,您的代码永远不会被触发。看这个:Firebase Notification
您需要做的是检查应用程序的启动方式,点击通知或点击启动器图标。为此,应将一些数据添加到通知中,然后在应用程序的第一次启动 activity 中检索它们。如果您能成功获取它们,则意味着您的应用已通过点击通知启动,然后您就可以做您想做的事了。
我在我的 Android 应用程序中实施了 Firebase 通知。当我的应用程序是 运行 时,通知以我的自定义布局显示,但当应用程序不是 运行 时,通知以默认布局显示。当应用程序不是 运行 时,如何将通知布局更改为我的布局。此外,我存储共享首选项以让用户切换通知。但是当应用程序不是 运行 时,无论如何都会显示通知。如何实现?
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(SettingsFragment.getReceiceNotification()){ //if user wants to receive notification
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.push_notification_layout);
remoteViews.setImageViewResource(R.id.push_notif_icon,R.mipmap.ic_bird_black);
Intent intent = new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContent(remoteViews);
notificationBuilder.setContentTitle("Radyo Türkkuşu");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
remoteViews.setTextViewText(R.id.push_title, "Radyo Türkkuşu");
remoteViews.setTextViewText(R.id.push_context, remoteMessage.getNotification().getBody());
//notificationBuilder.setLights (ContextCompat.getColor(MainActivity.context, R.color.pushColor), 5000, 5000);
notificationManager.notify(0,notificationBuilder.build());
}
}
你的问题是你将它与通知托盘一起使用。
看到这个link
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
如果您在应用程序处于后台时将 {data:"something"}(data-message)
与 {notification:"something"}(display-message)
一起使用,则数据负载将传递给 intent 的额外部分,但不会传递给 onMessageReceived()
method.I 假设你实现了显示通知的代码,所以当你的应用程序在前台时 onMessageReceived()
被触发并且它显示你想要的期望通知但是当它不是 onMessageReceived()
时没有获取触发器而不是 android 系统将使用您的通知负载处理它。您只需从服务器端代码中删除 {notification:"something"}(display-message/notification tray)
即可始终确保 onMessageReceived()
.
对于任何人保持提及 onMessageReceived()
将始终触发无论它是否是前景或背景请访问此 link
FCM 消息有两种类型
- 通知消息
- 数据消息
从 Firebase 控制台发送的消息是通知消息。为了在 onMessageReceived() 中获取消息,请使用 Data Message。在服务器端使用以下代码发送数据通知消息
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
引用https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
当您的应用关闭或 运行 在后台时,您的代码永远不会被触发。看这个:Firebase Notification
您需要做的是检查应用程序的启动方式,点击通知或点击启动器图标。为此,应将一些数据添加到通知中,然后在应用程序的第一次启动 activity 中检索它们。如果您能成功获取它们,则意味着您的应用已通过点击通知启动,然后您就可以做您想做的事了。