我使用 fcm 实现了平视通知。
当应用程序收到 fcm 通知时,如果我的应用程序正在运行,则平视通知会显示在屏幕上。那挺好的。
但如果我的应用程序处于后台或被杀死,则不会显示平视通知。
我怎么解决这个问题?
(也许我认为如果我的应用程序在收到 fcm 通知时正在运行,MyFirebaseMessagingService 工作良好。但是如果我的应用程序处于后台或被杀死,MyFirebaseMessagingService 类将不起作用)
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage message) {
Intent push = new Intent(this, SplashActivity.class);
push.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle("test")
.setContentText(message.getNotification().getBody())
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVibrate(new long[] {0})
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(fullScreenPendingIntent);
NotificationManager nm =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, builder.build());
}
}
有两种类型的 FCM 消息可用。
通知消息,有时被认为是“显示消息”。
数据消息,由客户端应用程序处理。
通知消息不会显示。当您的应用程序未运行或被终止时。
请检查下面的链接,它会对您有所帮助。
https://firebase.google.com/docs/cloud-messaging/concept-options https://firebase.google.com/docs/cloud-messaging/concept-options
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)