Mihir Shah
Mihir Shah

Reputation: 1809

FCM android onmessagereceived not called

I have implemented FCM in the app. Other firebase features working fine i.e. uploading to firebase storage and firebase real time messaging. But when I send push notification 1st time to the device, it shows successfully sent notification, but on messagereceived not called. then immediately when i sent another push notification, it shows not registered. and it always not registered then after.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("myLog", "From: " + remoteMessage.getFrom());
    Log.d("myLog", "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}

app.gradle:

apply plugin: 'com.google.gms.google-services'

project.gralde:

classpath 'com.google.gms:google-services:3.1.0'

manifest:

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

Device token is refreshed correctly, when ever i clear data and reopen app, it immediately print new device token.

Upvotes: 1

Views: 2001

Answers (3)

vishnuc156
vishnuc156

Reputation: 970

onMessageReceived(RemoteMessage remoteMessage) method called based on the following cases.

  • FCM Response With notification and data block:

{   "to": "device token list",   "notification": {     "body": "Body of Your Notification",     "title": "Title of Your Notification"   },   "data": {     "body": "Body of Your Notification in Data",     "title": "Title of Your Notification in Title",     "key_1": "Value for key_1",     "image_url": "www.abc.com/xyz.jpeg",     "key_2": "Value for key_2"   } }

  1. App in Foreground:

onMessageReceived(RemoteMessage remoteMessage) called, shows LargeIcon and BigPicture in the notification bar. We can read the content from both notification and data block

  1. App in Background:

onMessageReceived(RemoteMessage remoteMessage) not called, system tray will receive the message and read body and title from notification block and shows default message and title in the notification bar.

  • FCM Response With only data block:

In this case, removing notofocation blocks from json

{   "to": "device token list",   "data": {     "body": "Body of Your Notification in Data",     "title": "Title of Your Notification in Title",     "key_1": "Value for key_1",     "image_url": "www.abc.com/xyz.jpeg",     "key_2": "Value for key_2"   } }

  1. App in Foreground:

onMessageReceived(RemoteMessage remoteMessage) called, shows LargeIcon and BigPicture in the notification bar. We can read the content from both notification and data block

  1. App in Background:

onMessageReceived(RemoteMessage remoteMessage) called, system tray will not receive the message because of notification key is not in the response. Shows LargeIcon and BigPicture in the notification bar

Code

 private void sendNotification(Bitmap bitmap,  String title, String 
    message, PendingIntent resultPendingIntent) {

    NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
    style.bigPicture(bitmap);

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

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = mContext.getString(R.string.default_notification_channel_id);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);

        notificationManager.createNotificationChannel(notificationChannel);
    }
    Bitmap iconLarge = BitmapFactory.decodeResource(mContext.getResources(),
            R.drawable.mdmlogo);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.mdmlogo)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setSound(defaultSound)
            .setContentText(message)
            .setContentIntent(resultPendingIntent)
            .setStyle(style)
            .setLargeIcon(iconLarge)
            .setWhen(System.currentTimeMillis())
            .setPriority(Notification.PRIORITY_MAX)
            .setChannelId(NOTIFICATION_CHANNEL_ID);


    notificationManager.notify(1, notificationBuilder.build());


}

Reference Link:

https://firebase.google.com/docs/cloud-messaging/android/receive

Upvotes: 1

Hardik Mehta
Hardik Mehta

Reputation: 2415

its depend on what type of Message types coming from console. if its only Notification messages then if your app is closed then your receiver will not fired. if its with Notification messages and Data messages then still your app can handle this messages when app is in foreground

make sure that you are using this dependency compile 'com.google.firebase:firebase-messaging:10.2.1'

Upvotes: 0

Nakul Pritam
Nakul Pritam

Reputation: 76

What device are you using for testing this? Some manufacturers like Xiaomi and Leeco are known to prevent apps from receiving FCM notifications.

In case of devices like Samsung, Motorola: Make sure you are handling the right kind of Message type. There are 2 types of FCM messages you can send.

  1. Notification Messages
  2. Data Messages

Read this page for further information.

https://firebase.google.com/docs/cloud-messaging/concept-options

Upvotes: 0

Related Questions