繁体   English   中英

带有新Firebase Cloud Messaging系统的通知图标

[英]Notification Icon with the new Firebase Cloud Messaging system

昨天,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通知的限制。 当应用程序在后台运行时,将从清单(带有必要的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发送消息,也可以始终自定义图标。

使用服务器实现将消息发送到客户端,并使用消息的数据类型而不是消息的通知类型。

无论您的应用程序是后台还是前台,这都将帮助您获取onMessageReceived的回调,然后可以生成自定义通知,然后

atm他们正在解决这个问题https://github.com/firebase/quickstart-android/issues/4

当您从Firebase控制台发送通知时,默认情况下会使用您的应用程序图标,而Android系统在通知栏中时会将其变为白色。

如果您对该结果不满意,则应实现FirebaseMessagingService并在收到消息时手动创建通知。 我们正在努力改善这一点,但目前这是唯一的方法。

编辑:使用SDK 9.8.0添加到AndroidManifest.xml

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

我的解决方案类似于ATom的解决方案,但更易于实现。 您无需创建完全遮盖FirebaseMessagingService的类,只需覆盖接收Intent的方法(至少在9.6.1版中是公共的),然后从其他内容中获取要显示的信息。 “ hacky”部分是方法名称确实被混淆,并且每次将Firebase sdk更新到新版本时都会更改,但是您可以通过使用Android Studio检查FirebaseMessagingService并查找需要一个Intent作为唯一参数。 在版本9.6.1中,它称为zzm。 我的服务如下所示:

public class MyNotificationService extends FirebaseMessagingService {

    public void onMessageReceived(RemoteMessage remoteMessage) {
        // do nothing
    }

    @Override
    public void zzm(Intent intent) {
        Intent launchIntent = new Intent(this, SplashScreenActivity.class);
        launchIntent.setAction(Intent.ACTION_MAIN);
        launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* R    equest code */, launchIntent,
                PendingIntent.FLAG_ONE_SHOT);
        Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),
                R.mipmap.ic_launcher);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notification)
                .setLargeIcon(rawBitmap)
                .setContentTitle(intent.getStringExtra("gcm.notification.title"))
                .setContentText(intent.getStringExtra("gcm.notification.body"))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

只需将targetSdkVersion设置为19。通知图标将变为彩色。 然后,等待Firebase解决此问题。

还有一种丑陋但可行的方法。 反编译FirebaseMessagingService.class并修改其行为。 然后只需将类放到应用程序中的正确包中,然后dex使用它而不是消息传递库本身中的类即可。 这很容易并且可以工作。

有方法:

private void zzo(Intent intent) {
    Bundle bundle = intent.getExtras();
    bundle.remove("android.support.content.wakelockid");
    if (zza.zzac(bundle)) {  // true if msg is notification sent from FirebaseConsole
        if (!zza.zzdc((Context)this)) { // true if app is on foreground
            zza.zzer((Context)this).zzas(bundle); // create notification
            return;
        }
        // parse notification data to allow use it in onMessageReceived whe app is on foreground
        if (FirebaseMessagingService.zzav(bundle)) {
            zzb.zzo((Context)this, intent);
        }
    }
    this.onMessageReceived(new RemoteMessage(bundle));
}

此代码来自9.4.0版,由于混淆,方法在不同版本中的名称将不同。

我正在通过FCM控制台并通过HTTP / JSON触发通知,结果相同。

我可以处理标题,完整消息,但图标始终是默认的白色圆圈:

通知屏幕截图

而不是代码中的自定义图标(setSmallIcon或setSmallIcon)或应用程序中的默认图标:

 Intent intent = new Intent(this, MainActivity.class);
    // use System.currentTimeMillis() to have a unique ID for the pending intent
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    if (Build.VERSION.SDK_INT < 16) {
        Notification n  = new Notification.Builder(this)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true).getNotification();
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //notificationManager.notify(0, n);
        notificationManager.notify(id, n);
    } else {
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        Notification n  = new Notification.Builder(this)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setLargeIcon(bm)
                .setContentIntent(pIntent)
                .setAutoCancel(true).build();

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //notificationManager.notify(0, n);
        notificationManager.notify(id, n);
    }

如果您的应用程序在后台,则通知图标将设置为onMessage Receive方法,但如果您的应用程序在前台,则通知图标将为您在清单中定义的图标。

在此处输入图片说明

写这个

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

右下<application.....>

在此处输入图片说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM