firebase / quickstart-android

Firebase Quickstart Samples for Android
https://firebase.google.com
Apache License 2.0
8.82k stars 7.32k forks source link

Custom Notifications when app is in background or killed #4

Closed ckarthikv closed 8 years ago

ckarthikv commented 8 years ago

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.

samtstern commented 8 years ago

@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 commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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 notifications@github.com 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 https://github.com/firebase/quickstart-android/issues/4#issuecomment-220697618

samtstern commented 8 years ago

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 commented 8 years ago

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 notifications@github.com 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 https://github.com/firebase/quickstart-android/issues/4#issuecomment-220710992

samtstern commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

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 notifications@github.com 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 https://github.com/firebase/quickstart-android/issues/4#issuecomment-221375176

samtstern commented 8 years ago

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 notifications@github.com 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 https://github.com/firebase/quickstart-android/issues/4#issuecomment-221375176

kroikie commented 8 years ago

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

thedumbtechguy commented 8 years ago

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 notifications@github.com 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 notifications@github.com 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 < https://github.com/firebase/quickstart-android/issues/4#issuecomment-221375176

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/firebase/quickstart-android/issues/4#issuecomment-221385340

kroikie commented 8 years ago

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

thedumbtechguy commented 8 years ago

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 notifications@github.com 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 https://github.com/firebase/quickstart-android/issues/4#issuecomment-221410342

bosbefok commented 8 years ago

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 https://github.com/firebase/quickstart-android/issues/8

Svoka commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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

bosbefok commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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

thedumbtechguy commented 8 years ago

I'd prefer we simply got the opportunity to handle all notifications if possible like we did with GCM. It can be a configuration item as well.

I'm currently not using FCM to push notifications due to it being unpredictable atm.

On Fri, May 27, 2016 at 9:24 AM, Artem Osipov notifications@github.com wrote:

@sir-gon https://github.com/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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-android/issues/4#issuecomment-222101230, or mute the thread https://github.com/notifications/unsubscribe/AA4XZcaeLgZSGWEWC7nuNovN3nQiTZswks5qFrhWgaJpZM4IiXTl .

Regards, Stefan Froelich.

Svoka commented 8 years ago

@frostymarvelous but when app in background GCM also send it to notification tray, not in app.

kroikie commented 8 years ago

@frostymarvelous to be clear @Svoka is correct, FCM handles messages in the exact same way as GCM. Notification messages have always been handled in this way. If you always want to handle all notifications then you should send data only messages and handle the creation and display of notifications yourself. Note however that messages from the Firebase console are notification messages.

thedumbtechguy commented 8 years ago

Thanks for clarifying that. Again. I will give it another spin this weekend!

antoineF commented 8 years ago

The problem of using data notification instead of notification message is that it is not compatible with iOS. For exemple, if you want to send a message to a topic to both iOS and Android it's hard to find way to be compatible to the both platforms : Send only data payloads -> message is not sent to iOS, Send notification payloads (with or without data payloads) -> white icon on android.

The tip of @Svoka solves the bug for now, but it's quite a hack!...

kroikie commented 8 years ago

@antoineF thanks for the feedback however:

yayaa commented 8 years ago

Sending notifications from console is really powerful feature thanks for that, but since it is not possible to customize notifications when application is in background, as @frostymarvelous said would it be possible to create a configuration item or something like that and handle it in FCM? And maybe we could have previews of notification how would it be seen on devices?

Setting a bigImage, defining action buttons, small icon, and even wear optimisations... by this configuration item.

Then it would be an amazing feature to use, but right now it forces not to use :( (Because you know customer NEVER gets enough with default behaviours and they ALWAYS need custom stuff :))

kroikie commented 8 years ago

Thanks for the feedback @yayaa it is noted!

thedumbtechguy commented 8 years ago

Couldn't have said it better myself. The dashboard is a really powerful tool and I'm currently not even attempting to use it.

AndreasZeiser commented 8 years ago

@kroikie Can you please clarify when we can expect a fix? Sending notifications through the Firebase console was the main reason for us to jump on the Firebase train.

Also, I want to point out that I'm not able to fully customize a notification inside onMessageReceived callback. What I do is simple: build a notification with NotificationCompat with title, body and a small icon from my drawables. When the notification gets shown, the icon is transparent. If I use the launcher icon instead it shows up as expected.

pinkeshdarji commented 8 years ago

I am developing movie review app where I need to push a message with movie title and movie poster so for that I need to implement NotificationCompat.BigPictureStyle regardless of app state(Foreground /Background) BUT i found that this could not happen when app is in the background with firebase cloud messaging.

There must be some mechanism like this: public void onMessageReceived(RemoteMessage remoteMessage) { // Determine app state (foreground/background) // if(foreground){ // use notification style 1 //} //else(){ // use notification style 2 // } }

After reading entire page I have decided NOT to use firebase as of now, I will implement GCM again. I hope this gets resolved as soon as possible.I am big fan of firebase.

SplinterII commented 8 years ago

I'm still such a noob, but via Raspberry Pi, Node-red and Firebase I now receive a notification on my phone when someone presses my doorbell. Helped by this page, even when the notification sample app is in the background, I get vibs or a tone. What I prefer though is, when the app is in the background and someone presses my doorbell I get a continuing ringtone, just like when someone would call me. This would be 'easy' when the app is in the foreground, but how to do it when it's in the background? From reading above I believe this cant be done yet, right?

kroikie commented 8 years ago

@AndreasZeiser While I'm not able to give you a date for the fix to the icon issue, I can say that this is a high priority for the FCM team, and a solution will be released ASAP. I'm not sure why your notification icon is coming up transparent, once you receive the onMessageReceived callback you should be able to create and show notifications in the standard way, using NotificationCompat like you are.

@pinkeshdarji It may be worth noting that FCM and GCM receive notification messages the same way. So the foreground/background logic (which only applies to notification messages) will apply whether you are using FCM or GCM. I think that using FCM with data only messages will accomplish what you want since data only messages result in onMessageReceived being called whether the app is in the foreground or the background. The app being in the foreground or the background is only an issue when sending/receiving notification messages.

@NL-Splinter In cases when you want to have a callback to onMessageReceived whether the app is in the foreground or background then you should send data only messages.

SplinterII commented 8 years ago

Hi Arthur,

Thanks to your comment I now understand how to send a data only message, and the example app is indeed responding accordingly.

The only thing I don’t understand is:

the data payload is delivered in the extras of the intent of your launcher Activity

This kind of language is beyond my current Android skills :(

And this is not the way right? (I’m getting null as a msg)

private void sendNotification(String messageBody) { Intent intent = new Intent(this, MainActivity.class); Log.d(TAG, "Msg: " + intent.getStringExtra("score"));

In other words: where and how do I pick up the (key/)value pair sent via the data?

I’m guessing it’s just 1 line of code, which I hope isn’t too much asked.

Thanks anyway for your help sofar!!

Regards, Jan

Van: Arthur Thompson [mailto:notifications@github.com] Verzonden: woensdag 1 juni 2016 22:26 Aan: firebase/quickstart-android quickstart-android@noreply.github.com CC: NL-Splinter janhein@zonnet.nl; Mention mention@noreply.github.com Onderwerp: Re: [firebase/quickstart-android] Custom Notifications when app is in background or killed (#4)

@AndreasZeiser https://github.com/AndreasZeiser While I'm not able to give you a date for the fix to the icon issue, I can say that this is a high priority for the FCM team, and a solution will be released ASAP. I'm not sure why your notification icon is coming up transparent, once you receive the onMessageReceived callback you should be able to create and show notifications in the standard way, using NotificationCompat like you are.

@pinkeshdarji https://github.com/pinkeshdarji It may be worth noting that FCM and GCM receive notification messages the same way. So the foreground/background logic (which only applies to notification messages) will apply whether you are using FCM or GCM. I think that using FCM with data only messages will accomplish what you want since data only messages result in onMessageReceived being called whether the app is in the foreground or the background. The app being in the foreground or the background is only an issue when sending/receiving notification messages.

@NL-Splinter https://github.com/NL-Splinter In cases when you want to have a callback to onMessageReceived whether the app is in the foreground or background then you should send data only messages.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-android/issues/4#issuecomment-223113956 , or mute the thread https://github.com/notifications/unsubscribe/AFqfSgm0rzEqP2kVtNeYCpKbQHR05yc-ks5qHeregaJpZM4IiXTl .

pinkeshdarji commented 8 years ago

Thanks @kroikie, onMessageReceived being called whether the app is in the foreground or the background by just passing data only messages as you suggested.

Now I can handle data like this- public void onMessageReceived(RemoteMessage remoteMessage) { String topic = remoteMessage.getFrom(); String movieId = remoteMessage.getData().get("movieId"); String title = remoteMessage.getData().get("title"); String details = remoteMessage.getData().get("details"); String movieUrl = remoteMessage.getData().get("movieUrl");

    sendNotification(movieId,title,details,movieUrl);

}

In sendNotification I can create custom notification. Thanks Once again.

SplinterII commented 8 years ago

Super, helped me a lot!

I'm using this now to get the whole key/value set:

        for (String key : remoteMessage.getData().keySet()) {
            Log.d(TAG, key + ": " + remoteMessage.getData().get(key));
        }
swatijiet commented 8 years ago

Does data only message means the message text on the top has to remain empty and only key value pairs in advanced options are to be filled? I am still getting a totally white color icon on lollipop and above.

screen shot 2016-06-03 at 7 43 58 am

Svoka commented 8 years ago

@swatijiet you can't send data-only messages from Console at the moment. You need to use API

gauravjain131 commented 8 years ago

Hi, does the click_action work from the console when app is in background? Or the APIs need to be used for that also? It works perfectly in foreground case, but not able to get it work in background. Is it a bug or behavior?

seyfullahbilgin commented 8 years ago

I have same problem. I tried to use different android service that only get fcm message body string to create custom notification but the same behavior appeared. My notification icon turned to white icon at the background.

Please fix the issue asap. :)

martinodecarlo commented 8 years ago

I had the same issue (the blank icon).

So I tried to use a POST request to https://fcm.googleapis.com/fcm/send through cURL in this way:

curl -i -H "Authorization: key=MY_SERVER_KEY" -H "Content-Type: application/json" -d @nt.json https://fcm.googleapis.com/fcm/send

Now I want to send the notification to all the devices, without specifying the token of each user. However if I don't specify the "to" element in the JSON file, I get error 400.

How can I send my data to all users without specifying their token?

xo0ps commented 8 years ago

I have the same problem. I need to store notification in db for late use and also change the notification style and couple of more works on the received notification which is not possible. Please fix this issue as soon as possible.