Custom Notifications when app is in background or killed
Hi,
I'd like to implement custom notification styles when app is in background or is killed. Is there a Firebase broadcast receiver class one could extend to implement the same? Would be very helpful. Thanks.
@Svoka you used "notification" as the key, wouldn't it be "data..?? I got this from docs..
https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": { "score": "5x1", "time": "15:10" }, "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." }
@xainbutt yes, you're right. This code needs to to do a little modification if you want to send data-messages.
I wonder if we don't face a very recent problem on Android about the ability to receive data-messages while the application is in background. It worked, for sure as I have a published application built 07/08/16 which perfectly receives data-message sent via API even if it's in background.
But I currently realize that the same project I rebuild now after some updates asked by Google within Studio, cause my application to not receive any data-message anymore while in background.
And that's very new. Could anyone confirm it or not?
I am still looking for a way to Customize the notification message , specifically changing the style to BigTextStyle when the app is in the background. I have a relatively large message which gets ellipsized and more often than not the app is in the background when the user receives a notification from our server.
@2ndGAB Using Firebase 9.2.1 and Play services 9.2.1 notifications arrive in background.
@hossein-85 use the notification fields inside the data field for notifying Android users and then you can make your custom notifications (if you use the notification field you won't get the background notification in your declared Messaging service on Android). Unfortunately this does not work for iOS users, so you have to send separate requests to the Firebase messaging service.
Kind of pain in the ass right now as the behaviour in Android & iOS are different. Android developer unable to make customization in Notification message. It's difficult to show something like (5 new messages from somebody) unless the app server do the calculation which is NOT a good way. Is there any better way other than sending (Data only message to Android & Notification message to iOS)?
@howyongjun if you want full control of the notification on Android then you should send a data-message and since data messages cannot be received while an iOS app is in the background, you need a notification-message for that, so you would have to send two different notifications to each platform.
I understand the desire to send the same message to both iOS and Android as you would not have to keep track of the platforms, thanks for the feedback on this.
@kroikie I got some question about the collapsible. The problem I'm having is I sent 4 different notification message with 4 different collapse_key while the device is offline, then i reconnect to Internet & ONLY receive the last notification message. Is this an issue or i misunderstood the meaning of collapsible?
Using Firebase 9.2.1 and Play services 9.2.1 notifications arrive in background.
@Gloix Do you mean data-only messages or are notifications created from Console working for you? Tested latest Firebase on Android and my app doesn't seem to receive notifications created in Console while in background.
@howyongjun what platform are you checking? collapse_key will not work if you are sending messages to iOS FCM does not hold on to messages sent to iOS devices.
@kroikie i'm using collapse_key for Android only. It seems that some of the behaviour are not the same as what mentioned in the document. For instance, disconnect device from Internet connection --> send different messages with different collapse_key --> reconnect device with Internet connection --> receive only the last message sent. Any idea on this behaviour?
@Gloix did you very recently build a project to verify it still work because It worked for me until begining of july but it don't work anymore for me after recent development environment updates.
Since i am not able to create a custom notification -as talked above- we decided to implement data-only messages via FCM API, and not use console -even though how powerful it is.
But just out of curiosity is there a way that FCM console messages displayed by using colorPrimary | colorAccent or any of resources? And also if the notification gets icon from R.mipmap.ic_launcher or it gets icon from application's icon?
Because i can't know how that automated notification is built, i just wanted to know if there is any resources that build configuration is currently getting data from? So that i may try a work around on those values to get a good look in case of we consider send notification from console in future. (Right now we do not even mention about it, because it displays a notification with gray background and a white rounded square icon)
@2ndGAB @tiagoshibata I'm sorry for the delayed response. I just tested with Firebase library 9.2.1 through the Firebase console and I received the notification in background.
@Gloix That's a good news. Can you tell me which Android Studio version do you use?
Edit But when you say that you receive notification in background, you mean the notification is received in System tray because AFAIK, Firebase console only send Notification message, maybe with data payload, I don't remember well and so, when the app is in background, notification is received by the system tray and data payload is passed to the application by an Intent when you tap the notification right?
@2ndGAB Android Studio 2.1.2. Maybe I should also mention that I included a title, a message and that the target was a single user. The target user was running Android Marshmallow.
@Gloix ok so no data payload message
@2ndGAB Exactly. No data message. I actually didn't include any additional fields in the Firebase console form. Only title, message (I included a message tag as well (the second field in the form) ) and the single target user. App was in background (closed, not open in background), but phone screen turned on.
halo i have same problem , my icon not show on background notifcation just show white background in bar and not popup like foreground notification i tested on lollipop device here is my http request
{
"collapse_key" : "demo", "priority" : "high", "data": { "id" : 123, "message": "kirim data pakan Kolam Siapapuns", "title": "Log Pakan", "type": "feed_log", "content": { "pond_id": 5, "pond_name": "Kolam Siapapun" }, }, "notification" : { "title" : "Title" "body" : "Haloooooo", "icon" : "myicon", "sound" : "default", "click_action" : "FISH_LOGS" }, "to" : "el4ol2TYUyg:APA91bG6YrtvgAdNHxLxxxXX" }
whats wrong with my request ?
@abdymalikmulky Are you supposed to receive your data message in onMessageReceive()?