如果您的应用程序在后台,则会在onMessage Receive方法上设置通知图标,但如果您的应用程序位于前台,则通知图标将是您在清单上定义的图标
只需将targetSdkVersion设置为19.通知图标将为彩色。 然后等待Firebase解决此问题。
我的解决方案类似于ATom的解决方案,但更容易实现。您不需要创建一个完全隐藏FirebaseMessagingService的类,您可以只覆盖接收Intent的方法(这是公共的,至少在版本9.6.1中),并从附加内容中显示信息。 “hacky”部分是方法名称确实是混淆的,并且每次将Firebase sdk更新为新版本时都会更改,但您可以通过使用Android Studio检查FirebaseMessagingService并查找需要的公共方法来快速查找一个Intent作为唯一的参数。在版本9.6.1中,它被称为zzm。 这是我的服务的样子:
public class MyNotificationService extends FirebaseMessagingService { public void onMessageReceived(RemoteMessage remoteMessage) { // do nothing } @Override public void zzm(Intent intent) { Intent launchIntent = new Intent(this, SplashScreenActivity.class); launchIntent.setAction(Intent.ACTION_MAIN); launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* R equest code */, launchIntent, PendingIntent.FLAG_ONE_SHOT); Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notification) .setLargeIcon(rawBitmap) .setContentTitle(intent.getStringExtra("gcm.notification.title")) .setContentText(intent.getStringExtra("gcm.notification.body")) .setAutoCancel(true) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } }
还有一种丑陋却又有效的方式。反编译FirebaseMessagingService.class并修改它的行为。然后将该类放在yout app中的正确包中,dex使用它而不是消息库本身中的类。这很容易和工作。
方法有:
private void zzo(Intent intent) { Bundle bundle = intent.getExtras(); bundle.remove("android.support.content.wakelockid"); if (zza.zzac(bundle)) { // true if msg is notification sent from FirebaseConsole if (!zza.zzdc((Context)this)) { // true if app is on foreground zza.zzer((Context)this).zzas(bundle); // create notification return; } // parse notification data to allow use it in onMessageReceived whe app is on foreground if (FirebaseMessagingService.zzav(bundle)) { zzb.zzo((Context)this, intent); } } this.onMessageReceived(new RemoteMessage(bundle)); }
此代码来自版本9.4.0,由于混淆,方法将在不同版本中具有不同的名称。
他们正在研究这个问题 https://github.com/firebase/quickstart-android/issues/4
当您从Firebase控制台发送通知时,默认情况下会使用您的应用图标,并且Android系统会在通知栏中将该图标变为纯白色。 如果您对该结果不满意,则应该实施FirebaseMessagingService并在收到消息时手动创建通知。我们正在努力改善这一点,但目前这是唯一的方法。
当您从Firebase控制台发送通知时,默认情况下会使用您的应用图标,并且Android系统会在通知栏中将该图标变为纯白色。
如果您对该结果不满意,则应该实施FirebaseMessagingService并在收到消息时手动创建通知。我们正在努力改善这一点,但目前这是唯一的方法。
编辑:用SDK 9.8.0添加 的 AndroidManifest.xml中 强>
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/my_favorite_pic"/>
我正在通过FCM控制台和HTTP / JSON触发我的通知......结果相同。
我可以处理标题,完整的消息,但图标始终是默认的白色圆圈:
通知截图
而不是代码中的自定义图标(setSmallIcon或setSmallIcon)或应用程序中的默认图标:
Intent intent = new Intent(this, MainActivity.class); // use System.currentTimeMillis() to have a unique ID for the pending intent PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0); if (Build.VERSION.SDK_INT < 16) { Notification n = new Notification.Builder(this) .setContentTitle(messageTitle) .setContentText(messageBody) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pIntent) .setAutoCancel(true).getNotification(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //notificationManager.notify(0, n); notificationManager.notify(id, n); } else { Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); Notification n = new Notification.Builder(this) .setContentTitle(messageTitle) .setContentText(messageBody) .setSmallIcon(R.drawable.ic_stat_ic_notification) .setLargeIcon(bm) .setContentIntent(pIntent) .setAutoCancel(true).build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //notificationManager.notify(0, n); notificationManager.notify(id, n); }