带有新 Firebase 云消息传递系统的通知图标

新手上路,请多包涵

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

}

原文由 marco 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 687
2 个回答

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

原文由 Ian Barber 发布,翻译遵循 CC BY-SA 3.0 许可协议

写这个

<meta-data
         android:name="com.google.firebase.messaging.default_notification_icon"
         android:resource="@drawable/ic_notification" />

右下 <application.....>

在此处输入图像描述

原文由 dmarquina 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题