【问题标题】:how to show BigPictureStyle Notification in android when app is in background while using firebase notifiaction?当应用程序在后台使用firebase通知时如何在android中显示BigPictureStyle通知?
【发布时间】:2019-09-12 11:37:33
【问题描述】:

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

为了处理应用在后台时未调用的 onMessageReceived,我在启动器活动中实现了重定向逻辑(通过意图包)。

但图像仍然不可见。

@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());

        }


    }

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

【问题讨论】:

    标签: android firebase firebase-cloud-messaging


    【解决方案1】:

    使用您想在通知中显示的固定尺寸图片 如下(图片不能有多种尺寸)

             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" />
    

    【讨论】:

    • 我想显示来自服务器的宣传图片,而不是来自 mipmap 或 drawable
    猜你喜欢
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多