0

I'm having an issue with FCM on flutter. I have implemented messaging from my server so I'm storing my phone token for each user.

The thing is that when a user logs in for the very first time everything works properly, messages are being sent and user gets notified.

If I do not use the app during the weekend, on Monday I try to send a message by doing some actions on my app but messages are not being sent. I can see my token stored properly in my database.

I'm using firebase_messaging 2.1.0 for flutter.

This is how I get my token

 _fireBaseMessaging.getToken().then((token){
  _myPhoneToken = token;
});

1-I know token may change when:

  • App deletes Instance ID
  • App is restored on a new device
  • User uninstalls/reinstall the app
  • User clears app data

But none of this happens.

Any advice on how to handle this scenario? thanks in advance.

UPDATE enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Matias
  • 708
  • 10
  • 24
  • Did you try to send a notification with [FCM rest API](https://firebase.google.com/docs/cloud-messaging/http-server-ref) to see if it works? It's useful also to get the error feedback. I suggest to use [Postman](https://www.getpostman.com/) for this task. – shadowsheep Jan 14 '19 at 16:41
  • Yes, I was missing authorization key. I added it but still the same thing. – Matias Jan 14 '19 at 19:15
  • Yep I see ^^. Did you see that data message doesn’t work if your app is terminated. See the table in README.md or [here](https://pub.dartlang.org/packages/firebase_messaging). I suggest you to send notification and not data message if you only have title and body to show. This works also if the app is terminated. – shadowsheep Jan 14 '19 at 19:22
  • Also set priority:high if you want a fast delivery. Now I’m on the go (writing from my phone). I can show you a payload as soon as I’m in front of my laptop. But here you can see an [example](https://stackoverflow.com/questions/54002617/custom-sound-push-notification-does-not-work-flutter/54003722#54003722) – shadowsheep Jan 14 '19 at 19:26
  • Have you tried notification massege with high priority? Let me know. – shadowsheep Jan 14 '19 at 20:21
  • Yes, but did not work. I have updated the print capture. I believe it has something to do with getting or setting up previous token to be valid again. I have read in the docs that tokens have a limited lifetime, but do not know how to get them to be valid again after sometime. – Matias Jan 14 '19 at 20:37
  • So strange. When you use an invalid token you get an error. Btw you have an invalid json. You need a comma at the end of sound : default. – shadowsheep Jan 14 '19 at 20:50
  • @Matias if the token has changed, you should listen to `onTokenRefresh` method and replace the older existing token on your server with a new one. That might help if that's the case. Also take a look at: https://hackernoon.com/notifications-in-android-are-horribly-broken-b8dbec63f48a – ishaan Jan 14 '19 at 20:52
  • I'll let you know. – Matias Jan 14 '19 at 21:23

2 Answers2

4

Provided you have setup the FCM sdk the right way (but you said that it works the fist time you install the app, so I guess so).

Provided that you are sure that the device_token you are using is the one of the device on which you are expecting to receive the notification (check if it's still the same), you should get on this device your notification quite soon if you use "priority" : "high".

{
 "to" : "device_token",
 "priority" : "high",
 "notification" : {
     "sound": "default",
     "body" : "Test Notification body",
     "title": "Test Notification title"
 }
}

This method call

_firebaseMessaging.getToken().then((String token)

return always the new token even if it has been updated. So if you print this out on your device and you send a notification on this token without error, there's no reason why you should not get the token if the device has a valid internet connection active.

It's true that the device token can change during time. If you uninstall and reinstall the app, you can see the token will change and if you try to send a notification on the old one, you will get an error.

If instead the token will change during application lifetime, you can be notify on your server side by listening:

_firebaseMessaging.onTokenRefresh.listen((newToken) {
  _fcm_token = newToken;
  // send the new fcm to your server
});

So first of all I suggest you to be able to send a notification to a device with Postman. Check if the token you are using is still the one on the device. Then you can try to uninstall and reinstall the application and try to use the old token. You will get an error. Then try to send to the new one, and you should get your notification.

Then wait for some days and try again, check if the token has changed or not and if it's not changed you should be able to send the notification without problems with the same token.

Also be aware that data message on Android if the app is terminated are still not supported.

shadowsheep
  • 14,048
  • 3
  • 67
  • 77
0

Some networks/router/mobile can cut the connection between firebase library and firebase server due to inactivity (5min without message). This cut may be detected by the library up to 30min (FCM heatbeat interval).

These are some links discussing this issue:

I contacted firebase support but they told that since the issue is caused by external part they cannot fix it (I suggest decreasing heartbeat interval ...)

I fixed it in android using an interval job which apply these instructions:

context.sendBroadcast(new Intent("com.google.android.intent.action.GTALK_HEARTBEAT")); context.sendBroadcast(new Intent("com.google.android.intent.action.MCS_HEARTBEAT"));

You may write this specific code for Android side and should find something similar for ios side.

ziriug
  • 1