通知图标与新的Firebase云消息传递系统 [英] Notification Icon with the new Firebase Cloud Messaging system

查看:705
本文介绍了通知图标与新的Firebase云消息传递系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天Google向Google I / O提交了基于新Firebase的新通知系统。我试过这个新的FCM(Firebase云消息传递),以Github上的例子为例。

通知的图标始终是 ic_launcher 尽管我已经声明了特定的可绘制的元素。为什么?

为什么?
这里是处理消息的官方代码

  public class AppFirebaseMessagingService extends FirebaseMessagingService {

/ **
*收到消息时调用。
*
* @param remoteMessage表示从Firebase云消息收到的消息的对象。
* /
// [START receive_message]
@Override $ b $ public void onMessageReceived(RemoteMessage remoteMessage){
//如果应用程序在前台处理,和通知消息在这里。
//同样,如果您打算根据收到的FCM
//消息生成您自己的通知,那么这里是应该启动的地方。请参阅下面的sendNotification方法。
sendNotification(remoteMessage);
}
// [END receive_message]

/ **
*创建并显示包含收到的FCM消息的简单通知。
*
* @param remoteMessage FCM RemoteMessage收到。
* /
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);

//这是我的插入寻找解决方案
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)$ b $ .setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 / *通知的ID * /,notificationBuilder.build());


$ b


解决方案

不幸的是,这是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发送消息,您也可以始终自定义图标。

Yesterday Google presented at Google I/O the new notification system based on the new Firebase. I tried this new FCM ( Firebase Cloud Messaging ) with the example on Github.

The icon of the notification is always the ic_launcher despite I have declared a specific drawable

Why ? Here below the official code for handling the message

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());
    }

}

解决方案

Unfortunately this was a limitation of Firebase Notifications in SDK 9.0.0-9.6.1. When the app is in the background the launcher icon is use from the manifest (with the requisite Android tinting) for messages sent from the console.

With SDK 9.8.0 however, you can override the default! In your AndroidManifest.xml you can set the following fields to customise the icon and color:

<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" />

Note that if the app is in the foreground (or a data message is sent) you can completely use your own logic to customise the display. You can also always customise the icon if sending the message from the HTTP/XMPP APIs.

这篇关于通知图标与新的Firebase云消息传递系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆