Not receiving Cloud function FCM message in android 8

I have below Cloud function to send FCM, it is received successfully in Android 7, but I am not able to get it working in Android 8.

My cloud function lib versions are :

"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2"
var message = { 
                data : {  event: `${constants.NOTIF_EVENT_START_SESSION}` , payload : `${jsonData}`}
            }        
            var options = {
                priority: "high",
                timeToLive: 60 * 60 * 24
                };
            console.info(`Getting notification token for the user`)  

                const token = 'token_here'
                console.info(`Received notification token for the user ${token}`) 
                return admin.messaging().sendToDevice(token, message,options)
                .then((response) => {
                    console.info("Successfully sent notification of start session")
                }).catch(function(error) {
                    console.warn("Error sending notification of start session: " , error)
                })

This is perfectly in Android 7 and below, but this message is not received on Android 8! So I updated priority like below, as documentation suggests now :

    var message = { 
        data : {  event: `${constants.NOTIF_EVENT_START_SESSION}` , payload : `${jsonData}`}
        , android : { priority : "high" }
    } 

But adding this android -> priority here is giving me error in cloud function like below :

Error sending notification of start session: { Error: Messaging payload contains an invalid "android" property. Valid properties are "data" and "notification". at FirebaseMessagingError.Error (native)

What is going on here!

Update 1:

Thanks AL, I have replaced sendToDevice() to send(), and also sending token also along with message object Result : I get this error in cloud function :

Error sending notification of start session: { Error: Firebase Cloud Messaging API has not been used in project my_project_id before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/fcm.googleapis.com/overview?project=my_project_id then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

I will update again soon.

Update 2

I enabled this API for my project and after waiting for few mins, it has started showing notification in Android 7. But on Android 8, I do not receive FCM message (I have put breakpoint on onMessageReceived but it never gets hit). Below is my service code :

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyMessagingService";

    @Inject
    NotificationConstants constants;


    @Override
    public void onCreate() {
        super.onCreate();

        ((MyApplication) getApplication()).getAppComponent().inject(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            setupNotificationChannels();
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void setupNotificationChannels() {
        setupNotificationChannel(constants.SHARE_ID, constants.getShareName());
        setupNotificationChannel(constants.REQUEST_ID, constants.getRequestName());
        setupNotificationChannel(constants.OTHER_ID, constants.getOtherName());
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void setupNotificationChannel(String id, String name) {
        NotificationManager mgr = ((NotificationManager) (getSystemService(NOTIFICATION_SERVICE)));
        NotificationChannel notificationChannel = new NotificationChannel(id, name, mgr.IMPORTANCE_HIGH);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.GREEN);
        notificationChannel.setShowBadge(true);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        mgr.createNotificationChannel(notificationChannel);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
    .....
    }

Anyone having a clue about what can go wrong?

Upvotes: 1

Views: 1652

Answers (1)

Ok this is how I got it working :

Firebase release notes say : FirebaseInstanceId service was deprecated, around 28 June 2018, and my tokens were update in subclass of that service. https://firebase.google.com/support/release-notes/android

While looking in my database I noticed that my firebase token for Android 7 phone was same, but firebase token for Android 8 phone had changed, but my cloud functions could not pickup latest token as app could not send back updated token.

So I implemented onNewToken method of FirebaseMessagingService and now my token is updated, and I get FCM messages on Android O.

Upvotes: 1

Related Questions