昨天,Google在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());
}
}
发布于 2016-05-20 03:34:38
不幸的是,这是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发送消息,您也可以随时自定义图标。
发布于 2016-05-28 02:19:16
使用服务器实现向客户端发送消息,并使用data类型的消息而不是通知类型的消息。
这将帮助您获得对onMessageReceived
的回调,无论您的应用程序是在后台还是前台,然后您都可以生成自定义通知
发布于 2016-05-26 21:57:12
他们正在处理那个问题https://github.com/firebase/quickstart-android/issues/4
当你从Firebase控制台发送通知时,
默认使用你的应用程序图标,当在通知栏中时,安卓系统会将该图标变为纯白。
如果您对结果不满意,那么您应该实现FirebaseMessagingService,并在收到消息时手动创建通知。我们正在努力改进这一点,但目前这是唯一的方法。
编辑:使用SDK9.8.0添加到AndroidManifest.xml
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/my_favorite_pic"/>
https://stackoverflow.com/questions/37325051
复制相似问题