Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Notifications when app is in background or killed #4

Closed
ckarthikv opened this issue May 19, 2016 · 143 comments
Closed

Custom Notifications when app is in background or killed #4

ckarthikv opened this issue May 19, 2016 · 143 comments

Comments

@ckarthikv
Copy link

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 ckarthikv changed the title Custom Notifications when app is in background Custom Notifications when app is in background or killed May 19, 2016
@samtstern
Copy link
Contributor

@ckarthikv could you clarify this question a little bit? Are you trying to show different notification styles when your app is in the background versus when your app is running in the foreground?

@ashokmachineni
Copy link

Hi,
When i tried to implement this notification i get error like
Error:(28, 0) Plugin with id 'com.google.gms.google-services' not found.

@ckarthikv
Copy link
Author

@samtstern - Yes, we wanted to implement custom notification styles like Big Picture style, etc, when the app is in background or when the app is killed. I believe the notifications would be working through a broadcast receiver?

Would be great if you could point me in the right direction so that one could intercept all kinds of messages (probably by extending the FirebaseBroadcastReceiver class) - whether the app is in background or killed, so that we could customize every aspect of the notification styles immaterial of the app state.

@thedumbtechguy
Copy link

Same problem. The icon being used is not what I want.
It is using the launcher icon while but the new guidelines require that we use a white on transparent icon. Also, the color is wrong.
I can't seem to find any way to customize this looking at the available methods.

Any help?

@samtstern
Copy link
Contributor

@ckarthikv you can see this example of FirebaseMessagingService which is where your app can receive FCM messages and react. This is a good place to create notifications.

Telling if your Activity is in the foreground (and reacting differently) can be a little tricky. My favorite way is to register a local BroadcastReceiver in the Activity and then do a sendBroadcast from the service. You will get true from that method if your receiver is running (meaning your app is in the foreground), and false otherwise.

You can see an example of this (using GCM, but the logic is the same for FCM) here.

@samtstern
Copy link
Contributor

@frostymarvelous when you send a notification from the Firebase console is uses your app icon by default, and the Android system will turn that icon solid white when in the notification bar.

If you are unhappy with that result you should implement FirebaseMessagingService and create the notifications manually when you receive a message. We are working on a way to improve this but for now that's the only way.

@thedumbtechguy
Copy link

@samtstern Both comments are incorrect.

Custom messaging service is only called when the app is in the foreground.

Android does not turn the icon white. It keeps its color.

I've already implemented FirebaseMessagingService for my app when it's in the foreground.
What we are asking for is how to handle it when it's in the background.

I don't understand why the notification is created automatically when FCM is meant to replace GCM. It is totally incompatible with the current flow.

@samtstern
Copy link
Contributor

@frostymarvelous the behavior you are seeing sounds like a bug, in my apps the FirebaseMessagingService is definitely called when the app is in the background.

As far as the icon color, on recent versions of Android the system applies a white mask to the icon. What sort of device do you have and what version of Android is it running?

@thedumbtechguy
Copy link

An S6 running 5.1.1.

I think your behavior is the bug.

From the docs

"When your app is in the background on a user's device, notifications are
delivered to the system tray. When a user clicks on the notification, the
app launcher opens your app. If you want, you can also add client message
handling to receive notifications in your app when it is already in the
foreground on the user's device."

On Fri, May 20, 2016, 7:29 PM Sam Stern [email protected] wrote:

@frostymarvelous https://github.com/frostymarvelous the behavior you
are seeing sounds like a bug, in my apps the FirebaseMessagingService is
definitely called when the app is in the background.

As far as the icon color, on recent versions of Android the system applies
a white mask to the icon. What sort of device do you have and what version
of Android is it running?


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#4 (comment)

@samtstern
Copy link
Contributor

Sorry @frostymarvelous I misunderstood your situation, you are correct. When you send a notification from the Firebase console and your app is in the background it cannot be customized.

If you want custom background notifications you should use the FCM API to send data messages to your app and handle them in the FirebaseMessagingService where you can decide how the notification should look.

@thedumbtechguy
Copy link

If that works, then I'm good.
I thought it behaved the same across.

Thanks a lot for your help.
If you're part of the team, then thanks for your hard work as well.

On Fri, May 20, 2016, 8:30 PM Sam Stern [email protected] wrote:

Sorry @frostymarvelous https://github.com/frostymarvelous I
misunderstood your situation, you are correct. When you send a notification from
the Firebase console
and your app is in the background it cannot be
customized.

If you want custom background notifications you should use the FCM API to
send data messages to your app and handle them in the
FirebaseMessagingService where you can decide how the notification should
look.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#4 (comment)

@samtstern
Copy link
Contributor

@frostymarvelous ok thanks for your patience while I figured out what was going on.

Please feel free to open issues if you try this approach and have questions. Good luck!

@Svoka
Copy link

Svoka commented May 24, 2016

@samtstern I can't find an example of FCM API and how to manage the custom icon.
Do you have an example of this? I'm new to cloud messaging and I only find that there is a parameter icon, but no details how to implement it in my app
Thank you!

@samtstern
Copy link
Contributor

Here is the documentation for sending Notification messages.

The icon parameter can be used to specify a drawable within your app. If you want to use R.drawable.foo, just pass foo.

@Svoka
Copy link

Svoka commented May 24, 2016

@samtstern thank you for the doc link, really helpfull.

Console has custom params when sending a message,
must the custom icon work there too? ( In my test this is still the app icon, but I can do something wrong)

@thedumbtechguy
Copy link

It will only work if your app is in the foreground. Otherwise, you have to
use the the api.

On Tue, May 24, 2016, 7:48 PM Artem Osipov [email protected] wrote:

@samtstern https://github.com/samtstern thank you for the doc link,
really helpfull.

Console has custom params when sending a message,
must the custom icon work there too? ( In my test this is still the app
icon, but I can do something wrong)


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#4 (comment)

@samtstern
Copy link
Contributor

As far as I am aware it is not possible to customize the icon through the
console right now, but that is a high-priority feature for us going forward.

On Tue, May 24, 2016 at 12:48 PM Artem Osipov [email protected]
wrote:

@samtstern https://github.com/samtstern thank you for the doc link,
really helpfull.

Console has custom params when sending a message,
must the custom icon work there too? ( In my test this is still the app
icon, but I can do something wrong)


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#4 (comment)

@kroikie
Copy link
Contributor

kroikie commented May 24, 2016

The key value pairs currently available in the Firebase composer are for data message values only.

@thedumbtechguy
Copy link

Do we need to customize it through console? Can't we just add it as a
config parameter? Thanks would be better
Don't forget the colors as well please?

Once again, thanks for the great job you all are doing!

On Tue, May 24, 2016, 8:47 PM Sam Stern [email protected] wrote:

As far as I am aware it is not possible to customize the icon through the
console right now, but that is a high-priority feature for us going
forward.

On Tue, May 24, 2016 at 12:48 PM Artem Osipov [email protected]
wrote:

@samtstern https://github.com/samtstern thank you for the doc link,
really helpfull.

Console has custom params when sending a message,
must the custom icon work there too? ( In my test this is still the app
icon, but I can do something wrong)


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
<
#4 (comment)


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#4 (comment)

@kroikie
Copy link
Contributor

kroikie commented May 24, 2016

It would have to be through the console since the client side handler does not reference any of your specified remote config parameters.

@thedumbtechguy
Copy link

Well, I thought that would work through the Configuration in the
Application class. I wonder how that would work since we can't tell the
value of whatever R.drawable.foo will be.

On Tue, May 24, 2016, 9:51 PM Arthur Thompson [email protected]
wrote:

It would have to be through the console since the client side handler does
not reference any of your specified remote config parameters.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#4 (comment)

@bosbefok
Copy link

bosbefok commented May 25, 2016

I'm having a similar issue getting the notifications customized. I'm not using the console at all - only making API calls. When app is in foreground notifications get handled by my custom FirebaseMessagingService, but when app is in the background notification displays solid white icon and ignores any specified payload support parameters (icon, color etc).

Please advise whether this is expected as the behaviour appears to contradict comments made in this thread as well as #8

@Svoka
Copy link

Svoka commented May 25, 2016

@DespisedIcon I had the similar problem, but it was my mistake I set it in the data section and it must be in the notification.
Like this:

'to' => '_PHONE_ID_', "notification"=>array( 'title'=>"Test", 'body'=>"Test2", "icon"=>"ic_stat_ic_notification" // R.drawable.ic_stat_ic_notification )

@bosbefok
Copy link

@Svoka Thanks for the tip. I've moved the icon parameter into the notification and now I'm getting the message

W/FirebaseMessaging: Icon resource icon ic_stat_logo not found. Notification will use app icon.

This same icon (R.drawable.ic_stat_logo) is being used for foreground notification builder without any issues.

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_logo)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))

Any idea why Firebase cannot resolve the icon for background notification?

@kroikie
Copy link
Contributor

kroikie commented May 26, 2016

@DespisedIcon could you add your http request here maybe there is an issue with the formatting.

@bosbefok
Copy link

@kroikie You were right. I've tracked down this issue to malformed notification data in the request. Icon and color working correctly now. Thanks

@sir-gon
Copy link

sir-gon commented May 27, 2016

@Svoka thats work form me... but only for small icon... any idea for large icon?
(This method is not the desired way... y don't need to send a custom "icon" parameter for iOS)

@Svoka
Copy link

Svoka commented May 27, 2016

@sir-gon if you don't specify the custom icon, FCM will use the app icon.
I'm agree, if we can just set the default icon in some settings file, it'd be more convenient.

@otonieru
Copy link

Two years passed and we still dont have solution for this ?

My app built using ionic 3, and it can receive the notification message just fine through FCM when the app is closed/killed,

But it can't show the data message received. Mean i can't customise the notification style. I can't show the picture, can't show the button, and can't give user custom action.

Currently, user need to tap the notification in the tray, which will open the app, then only the second notification, containing the data message, appear.

Is there really no way to directly push the notification with data messages when the app is killed ?

@dehghani-mehdi
Copy link

dehghani-mehdi commented Jun 12, 2019

Why this issue is closed?!

BTW, about the first post, the user @CarmelRaj07 reacted with

thumbs down emoji
laugh emoji
hooray emoji
confused emoji
rocket emoji
eyes emoji

And I can tell [s]he reacted with thumbs up and heart too. what a mess!!

[S]He did it again with all available emojis for thedumbtechguy's comment too.

@hahashigehaha
Copy link

hahashigehaha commented Jun 12, 2019 via email

@dehghani-mehdi
Copy link

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

@mmohades
Copy link

mmohades commented Jul 3, 2019

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

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

@nitishbhardwaj1111
Copy link

hey @ckarthikv
actually i am facing a issue in IOS when the app is in background.
#608 Can you please have look into it,.

@erkkkkkkkkk
Copy link

erkkkkkkkkk commented May 4, 2021

git clone https://gerrit.googlesource.com/gitiles

@sfuqua
Copy link

sfuqua commented Feb 23, 2022

(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: firebase/firebase-android-sdk#2639

@sylvinrodz
Copy link

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

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

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

@sylvinrodz
Copy link

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

Solution Please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests