Reputation: 136
I am using firebase notification service in my app. But I am facing some challenges. I need to execute my code and set preference when firebase notification has come. Is there any way or callback to get that a firebase notification has come. I am not using the data message of FCM because I don't have any server. So, the problem is get notified from default notification manager of firebase. Thanks in advance.
Upvotes: 1
Views: 511
Reputation: 2968
Firebase cloud messaging works differently in case of foreground and background. This thing is clearly mentioned in the documentation:
If you want foregrounded apps to receive notification messages or data messages, you’ll need to write code to handle the
onMessageReceived
callback.
Reference: Receive messages in an Android app
So, right now as far as I know and according to the documentation the onMessageReceived
is only fired when the application is in the foreground. But one thing you can do is to play with broadcast receivers to achieve what you want.
Upvotes: 0
Reputation: 398
I've faced this problem recently and finally solved it.
To do this, you need to go to Notification in Firebase Console, and in the lower part, there's a part Advanced options. (See in Image 1). There, in Custom Data (See in Image 2), you can add your own Data as Key-pair values, that will always be received in onMessageReceived(), no matter what state the app is in, background or foreground.
Further clarification for others facing similar problem:
According to their doc here, onMessageReceived() is called both in the case of app being in Foreground and Background, but only when response is sent as bundle in Data message. So you need to send response in Data message. But when there's no server backend, it's not clear how to send response using Data message.
Upvotes: 0
Reputation: 9225
No, there is no way to have a guaranteed callback when you get a notification message. If the app is in the foreground the onMessageReceived callback will be fired but when the app is in the background the auto generated notification will be shown and no callback will be fired.
Unfortunately at this time the Firebase console does not allow data messages. So there is no way to do what you are asking without using a server of some kind.
Upvotes: 3
Reputation: 6107
Check this link firebase notification receive In onMessageReceived(RemoteMessage remoteMessage)
method execute your code.
Upvotes: 1