【发布时间】:2017-02-28 18:23:54
【问题描述】:
我使用 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());
}
}
【问题讨论】:
标签: android notifications