昨天,谷歌在 Google I/O 上展示了基于新 Firebase 的新通知系统。我通过 Github 上的示例尝试了这个新的 FCM(Firebase Cloud Messaging)。
通知的图标始终是ic_launcher,尽管我已经声明了特定的可绘制对象
为什么? 下面是处理消息的官方代码
public class AppFirebaseMessagingService extends FirebaseMessagingService {
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
sendNotification(remoteMessage);
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param remoteMessage FCM RemoteMessage received.
*/
private void sendNotification(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// this is a my insertion looking for a solution
int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.myicon: R.mipmap.myicon;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(icon)
.setContentTitle(remoteMessage.getFrom())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
不幸的是,这是 SDK 9.0.0-9.6.1 中 Firebase 通知的限制。当应用程序位于后台时,将使用清单中的启动器图标(具有必要的 Android 色调)来发送从控制台发送的消息。
但是使用 SDK 9.8.0,您可以覆盖默认值!在 AndroidManifest.xml 中,您可以设置以下字段来自定义图标和颜色:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/notification_icon" />
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/google_blue" />
请注意,如果应用程序位于前台(或发送数据消息),您完全可以使用自己的逻辑来自定义显示。如果从 HTTP/XMPP API 发送消息,您还可以随时自定义图标。
使用服务器实现向客户端发送消息,并使用 data 类型的消息而不是 notification 类型的消息。
这将帮助您获得回调
onMessageReceived
,无论您的应用程序是在后台还是前台,然后您都可以生成自定义通知
atm 他们正在解决这个问题 https://github.com/firebase/quickstart-android/issues/4
当您从 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"/>
我的解决方案与ATom的类似,但更容易实现。您不需要创建一个完全隐藏 FirebaseMessagingService 的类,您只需重写接收 Intent 的方法(该方法是公共的,至少在版本 9.6.1 中)并从 extras 中获取要显示的信息。 “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());
}
}
只需将 targetSdkVersion 设置为 19。通知图标将会着色。 然后等待 Firebase 修复此问题。
还有一种丑陋但有效的方法。反编译 FirebaseMessagingService.class 并修改其行为。然后只需将该类放入 yout 应用程序中的正确包中,然后 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版本,由于混淆,方法在不同版本中会有不同的名称。
我从 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);
}
我想添加一个答案,因为我的问题很简单但很难注意到。特别是,我在创建
com.google.firebase.messaging.default_notification_icon
时复制/粘贴了现有的元数据元素,它使用 android:value
标签来指定其值。这不适用于通知图标,一旦我将其更改为android:resource
,一切都会按预期工作。
将此添加到活动标签之外应用程序标签内:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/notification"
/>
并确保使用您的自定义分辨率将通知文件复制到所有目录 mipmap-* 中。