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

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

thedumbtechguy avatar May 24 '16 22:05 thedumbtechguy

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

bosbefok avatar May 25 '16 02:05 bosbefok

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

Svoka avatar May 25 '16 08:05 Svoka

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

bosbefok avatar May 25 '16 23:05 bosbefok

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

kroikie avatar May 26 '16 00:05 kroikie

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

bosbefok avatar May 26 '16 00:05 bosbefok

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

sir-gon avatar May 27 '16 00:05 sir-gon

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

Svoka avatar May 27 '16 09:05 Svoka

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 [email protected] 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.

thedumbtechguy avatar May 27 '16 10:05 thedumbtechguy

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

Svoka avatar May 27 '16 13:05 Svoka

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

kroikie avatar May 27 '16 17:05 kroikie

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

thedumbtechguy avatar May 27 '16 20:05 thedumbtechguy

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

antoineF avatar May 30 '16 13:05 antoineF

@antoineF thanks for the feedback however:

  • Data only payloads can be sent to iOS, see the content_available field in the reference docs. Also when the iOS app is connected to FCM (in the foreground) data messages are sent through the FCM connection and through APNs otherwise.
  • The white icon on Android is a known issue, the FCM team is working on a fix.

kroikie avatar May 30 '16 14:05 kroikie

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 :))

yayaa avatar May 30 '16 19:05 yayaa

Thanks for the feedback @yayaa it is noted!

kroikie avatar May 30 '16 23:05 kroikie

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

thedumbtechguy avatar May 31 '16 08:05 thedumbtechguy

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

AndreasZeiser avatar May 31 '16 15:05 AndreasZeiser

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.

pinkeshdarji avatar Jun 01 '16 09:06 pinkeshdarji

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?

SplinterII avatar Jun 01 '16 18:06 SplinterII