使用新的Firebase Cloud Messaging系统的通知图标

162

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

}

Firebase与您创建通知的方式无关,请提供您所看到的图像。 - tyczj
1
这段代码直接来自Firebase,sendNotification()方法对于任何通知都是完全相同的。这段代码在GCM上运行良好,但在FCM上不行。它始终保持为ic_launcher,在使用新的Web界面发送消息时。 - marco
@shinypenguin,你说得对!我没有注意到其他参数也不能自定义。是的,正如你所说,当应用程序在前台时图标可以工作。 - marco
1
http://codingaffairs.blogspot.com/2016/06/firebase-cloud-messaging-push.html - Developine
这可能是因为Firebase控制台发送了另一种结构的通知,而不是人们用于操作Android通知的数据...您是从控制台发送吗? - superUser
显示剩余2条评论
11个回答

0

我想在这里添加一个答案,因为我的问题很简单,但很难注意到。特别是在创建com.google.firebase.messaging.default_notification_icon时,我复制/粘贴了一个现有的元数据元素,它使用android:value标签来指定其值。这对于通知图标不起作用,一旦我将其更改为android:resource,一切都按预期工作。


同样适用于 default_notification_color 元数据,它需要是一个 android:resource - 设置为 android:value 将不起作用。 - flochtililoch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接