quickstart-android icon indicating copy to clipboard operation
quickstart-android copied to clipboard

Custom Notifications when app is in background or killed

Open ckarthikv opened this issue 10 years ago • 143 comments

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.

ckarthikv avatar May 19 '16 15:05 ckarthikv

@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..." }

ghost avatar Jul 14 '16 22:07 ghost

@xainbutt yes, you're right. This code needs to to do a little modification if you want to send data-messages.

Svoka avatar Jul 15 '16 07:07 Svoka

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?

2ndGAB avatar Jul 15 '16 09:07 2ndGAB

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.

hossein-85 avatar Jul 18 '16 18:07 hossein-85

@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.

Gloix avatar Jul 18 '16 19:07 Gloix

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 avatar Jul 21 '16 02:07 howyongjun

@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 avatar Jul 21 '16 02:07 kroikie

@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?

howyongjun avatar Jul 21 '16 07:07 howyongjun

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.

tiagoshibata avatar Jul 21 '16 14:07 tiagoshibata

@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 avatar Jul 21 '16 21:07 kroikie

@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?

howyongjun avatar Jul 22 '16 01:07 howyongjun

@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.

2ndGAB avatar Jul 22 '16 08:07 2ndGAB

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)

yayaa avatar Jul 26 '16 11:07 yayaa

@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 avatar Jul 26 '16 16:07 Gloix

@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 avatar Jul 26 '16 17:07 2ndGAB

@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 avatar Jul 26 '16 17:07 Gloix

@Gloix ok so no data payload message

2ndGAB avatar Jul 26 '16 17:07 2ndGAB

@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.

Gloix avatar Jul 26 '16 17:07 Gloix

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 ?

abdymm avatar Jul 27 '16 04:07 abdymm

@abdymalikmulky Are you supposed to receive your data message in onMessageReceive()?

2ndGAB avatar Jul 27 '16 06:07 2ndGAB