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

标签 android push-notification firebase googleio firebase-cloud-messaging

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

}

最佳答案

很遗憾,这是 SDK 9.0.0-9.6.1 中 Firebase 通知的限制。当应用程序在后台时,启动器图标会从 list (带有必要的 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 发送消息,您还可以始终自定义图标。

关于android - 带有新 Firebase 云消息传递系统的通知图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37325051/

相关文章:

swift - 应用待机 24 小时后未收到 Firebase 通知

c++ - 在 DLL 中使用的最简单的 C++ PUSH 通知/POST 解决方案?

android studio 没有启动 java.lang.NoSuchMethodError

java - IOException : Could not initialize SSL context - Apache Commons Net - Android

ios - phonegap-push-plugin 在 iOS 中不起作用

android - 找不到错误 : cannot access zzbfm class file for com. google.android.gms.internal.zzbfm

android - 编译时 build.gradle 错误 'com.firebase:firebase-client-android:2.5.0'

android - 使用kotlin更新特定时期的firebase实时数据库

android - 在通过 Google Sign In 使用 Firebase 应用程序时,如何通过 Google Cloud Function 进行身份验证?

android - 根上的 Firebase 引用查询