我有一个Firebase的FirebaseMessagingService类:
public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String message = remoteMessage.getNotification().getBody();
Map<String, String> params = remoteMessage.getData();
Bitmap remote_picture = null;
remote_picture = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo);
intent = new Intent(this, HomeDrawer.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle("Notification Title");
notificationBuilder.setContentText(message);
notificationBuilder.setSmallIcon(R.drawable.ic_notif);
notificationBuilder.setLargeIcon(remote_picture);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification();
noti = notificationBuilder.build();
//notificationBuilder.setVibrate(new long[] { 1000, 1000});
//notificationBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
notificationManager.notify(0,noti);
super.onMessageReceived(remoteMessage);
}
如果应用程序在后台,(未主动打开,但在后台,未关闭)通知smallIcon和LargeIcon没有出现,则默认显示为android,如果我更改
intent = new Intent(this, HomeDrawer.class);
到任何其他活动类,它不会打开我指定的类,并打开我在后台打开的最后一个类。
我做错了什么?如果背景,SmallIcon和LargeIcon无法正常工作。但是,通知消息显示正常,而且remoteMessage.getData();正确获取数据,这完全取决于图标和意图类的行为。
提前致谢。
答案 0 :(得分:0)
这种情况会发生,因为如果您的应用处于后台,通知会以不同方式处理。
如果应用程序在前台运行,那么您的FcmMessagingService
将被执行,并且您正在自行处理通知。
如果app没有运行,通知由android系统itseld处理。FcmMessagingService
永远不会被执行。对于这种情况,您必须在服务器上正确设置图标!
我相信自从引入doze
机制以来就会发生这种情况。
从服务器发送的通知应该看起来像这样
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon", // the name of the resource
"sound" : "mySound"
}