Firebase Expandable Notification Show image when a

2019-03-09 19:06发布

I am implementing FCM notifications in Android, but how does notifications differ depending on the app status (background vs. foreground)?

see notifications image

I am sending the notification using the FCM API with Postman and this is the notification structure:

{ "notification": {
      "title": "Notification title",
      "body": "Notification message",
      "sound": "default",
      "color": "#53c4bc",
      "click_action": "MY_BOOK",
      "icon": "ic_launcher"
   },
   "data": {
       "main_picture": "URL_OF_THE_IMAGE"  
   },
   "to" : "USER_FCM_TOKEN"
}

The image to render is taken from data.main_picture.

I have implemented my own FirebaseMessagingService which makes the notifications display perfectly in foreground state. The notification code is the next:

NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setSummaryText(messageBody);
notiStyle.bigPicture(picture);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(bigIcon)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setStyle(notiStyle); code here

NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());

However, in background, the service is not even executed. In the AndroidManifest.xml, Firebase services are declared as follow:

<service
    android:name=".MyFirebaseMessagingService">
  <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
  </intent-filter>
</service>

<service
    android:name=".MyFirebaseInstanceIDService">
  <intent-filter>
    <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
  </intent-filter>
</service>

My problem is not the LargeIcon or SmallIcon but displaying the big picture.

Thanks for your support.

8条回答
趁早两清
2楼-- · 2019-03-09 19:30

Send Big Picture notification from Firebase console : Works for both background and foreground app

Instead of onMessageReceived, override zzm() of FirebaseMessagingService and create your custom notification from here

@Override
public void zzm(Intent intent) {
    Log.e(TAG, "zzm : " + intent);
    createBigPictureNotification();        
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-09 19:33

If your problem is related to showing Big Image i.e. if you are sending push notification with an image from firebase console and it displays the image only if the app in the foreground. The solution for this problem is to send a push message with only data field.

{ "data": { "image": "https://static.pexels.com/photos/4825/red-love-romantic-flowers.jpg", "message": "Firebase Push Message Using API" "AnotherActivity": "True" }, "to" : "device id Or Device token" }

This definitely solves the problem.

查看更多
登录 后发表回答