使用Firebase通知时,当应用程序在后台时如何在Android中显示BigPictureStyle通知?

时间:2019-04-23 09:55:10

标签: android firebase firebase-cloud-messaging

我正在使用Firebase通知。当应用程序处于前台时,将调用onMessageReceived,并且可以看到带有图像的通知(BigPictureStyle notification)。但是当应用程序处于终止状态时,不会调用onMessageReceived,而是会调用带有标题和描述的基本通知。

要处理在应用程序处于后台时未调用的onMessageReceived,我已经在启动器活动中实现了重定向逻辑(通过intent bundle)。

但是图像仍然不可见。

@Override
 public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification(remoteMessage);
}
 private void sendNotification(RemoteMessage remoteMessage) {
        Intent intent;
        if (remoteMessage != null) {
            RemoteMessage.Notification notification = remoteMessage.getNotification();

            intent = new Intent(this, MainActivity.class);
            intent.putExtra("url", url);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            NotificationCompat.BigPictureStyle BigPicstyle = new NotificationCompat.BigPictureStyle()
                    .bigPicture(bitmap_image)
                    .setBigContentTitle(title)
                    .setSummaryText(body)
                    .bigLargeIcon(null);

            NotificationCompat.Builder notificationBuilder = null;


            notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
            if (bitmap_image != null) {
                notificationBuilder.setStyle(BigPicstyle);
                notificationBuilder.setLargeIcon(bitmap_image);
            } else {
                notificationBuilder.setStyle(BigTextstyle);
                notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
            }

            //region setting notification


            notificationBuilder
                   .setContentTitle(title)
                    .setContentText(body)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setWhen(System.currentTimeMillis())
                    .setVisibility(getNotificationId())
                    .setChannelId(CHANNEL_ID)
                    .setNumber(1)
                    .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                    .setContentIntent(pendingIntent);
            //endregion

            getManager().notify(getNotificationId(), notificationBuilder.build());

        }


    }

当应用处于终止状态时,我会收到有关标题和正文的基本通知。当应用程序处于终止状态时,我也如何获取图像?

1 个答案:

答案 0 :(得分:0)

使用您要在通知中显示的固定大小的图像 如下所示(图片不能使用多种尺寸)

         setLargeIcon(largeIcon);  // access it from drawable folder.

请勿在此处使用mipmap文件夹中的图像。它给出适当的分辨率图像。 在清单文件中做同样的事情...

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