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

(edit - shortened original post as it bordered on a rant, and while I tried to keep it polite I don't think it was productive)

@samtstern @kroikie pardon the tags but I think my scenario is impossible, by design, with the current Android SDK, given this thread and #368, and I'm hoping to confirm that (and if possible, get any insights into the why of the current SDK design).

I have a body of "legacy" existing notification payloads that get sent to both iOS and Android. For Android clients they were GCM originally, migrated to FCM, and on FCM they include a notification payload - so from an FCM standpoint, they are "notification messages with a data payload". Historically this has been fine, as these were very simple "title/body" type notifications, but I want to start making them "richer".

In order to start using Android rich notification features like styles and inline actions, I think my only option is to remove the notification payload and make these data-only. However, that's an expensive change for my team as the current notification templates are live "in the wild" with existing clients, so it's a breaking change (existing clients would stop showing the messages), so there needs to be a complicated transition/versioning story for the Android notifications delivered by our web service.

There seems to be clear developer desire for a way to handle this scenario purely with Android app code and no web service changes, given the two issues in this repo (plus all their dupes) and several high traffic StackOverflow posts.

FWIW this issue on the SDK covers the effective feature request here, but I don't see any discussion or responses in the last year - really all a dev needs is some way to tamper with the FCM message body before the SDK handles it, similar to a Notification Service Extension on iOS. That'd allow an app to "opt out" of the automatic SDK behavior on a per-notification basis, by deleting the notification payload if necessary, and not break existing app versions that might rely on it: https://github.com/firebase/firebase-android-sdk/issues/2639

sfuqua avatar Feb 23 '22 06:02 sfuqua

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.

I'm not sure if anyone has mentioned it, but of course it's possible to have custom notification! The only thing is that you should do it yourself! From the server, you only send the message payload (data message) in your push notification. Then in your phone side, like Android, you create the notification manually by using the information you receive in the push notification message payload. For example, this is a snippet that can work on your server, in Python:

from pyfcm import FCMNotification

api_key = "YOUR SERVER KEY"
registration_id = "Your user FCM registration token"
data_message = {
    "title": "ntf title",
    "body": "ntf body",
    "style": "a string that represent what you want, you will use it on the phone side."
}
push_service = FCMNotification(api_key=api_key)

result = push_service.notify_single_device(registration_id=registration_id, data_message=data_message)

print (result)

Then on Android side, you need to write your code for handling push notification. Basically what you can do is creating a notification, with title and body. Then for the style of the notification, you will decide according to style that you receive from FCM. It's completely up to you what to do, how to create the notification and its style.

Android example by Firebase, open the link to see the whole section.

Getting message Payload example

https://github.com/firebase/quickstart-android/blob/375c1ae5ec9000ee71b93cee409086e27d774bdb/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt#L43

Creating notification function (open the link to see sendNotification function)

https://github.com/firebase/quickstart-android/blob/375c1ae5ec9000ee71b93cee409086e27d774bdb/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt#L114

This will only work when app is foreground , when app is killed or in background it will not work

sylvinrodz avatar Jan 05 '24 17:01 sylvinrodz

@flyme2012 thank you, actually I found the solution. of course after posting my prev comment.

Solution Please

sylvinrodz avatar Jan 05 '24 17:01 sylvinrodz