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

@2ndGAB i have created my onMessageReceive() , that method just call when app in foreground this is my screenshoot

foreground notification (notification that i want) photo264167697778059618

background notification

  1. Just show the white background icon (even though my color parameter is works)
  2. not showing notification like in foreground photo264167697778059620

photo264167697778059619

i just confuse of FCM behavior , Would be very helpful. Thanks.

abdymm avatar Jul 27 '16 06:07 abdymm

@abdymalikmulky ok so if I understand well, when the application is in background, the Notification is received by the system tray and not your application. I never tried it so I cannot help you about the problem you have with your icon. I'm really sorry.

I'm not so experienced with FCM but the basics are the following.

When your application is in foreground, the notification and the data payload is received by onMessageReceive() as you do. When the applicaton is in background, there are 2 situations:

  • You send a Notification only message and it's received in the system tray and displayed as you observe but your application is not informed about it.
  • You send a Notification with a data payload like you do and the behavior is the one explained just above except that the data payload is passed to your application via the Intent you receive in your main activity when the user tap the notification which cause your app to be started.

But if you have time, I would be very interested that you try to create your notification in your application, based on data message only. That suppose to send the message from Google Advanced REST Client Google Advanced REST Client

Then you receive the data payload in onMessageReceive() as you certainly do already and you generate your notification from onMessageReceive() as you may do already but in case and just for a very simple test:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());

    sendNotification("FCM message received");

}

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

I would be very interested to know if a notification is created when you send the message while the application is in background. ic_stat_ic_notification (drawable hdpi)

ic_stat_ic_notification is attached.

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

Is it possible to use GCM receiver and FirebaseMessagingService's onMessageReceived(RemoteMessage remoteMessage){..} both at the same time.

It appears to me that when I send notification from Firbase console and app is in background... in my code both GCM receiver and FCM notification is showed.

KiDa-2049 avatar Sep 14 '16 09:09 KiDa-2049

first, is the notification showing icon app, sound and other same as property that you set in onMessageReceived() ? second , how if the app was killed ? the notification still show "normally" ?

abdymm avatar Sep 14 '16 09:09 abdymm

@abdymalikmulky yes... When App is in background firebase shows default notification as per documentaion.

In my code I have kept both GCM receiver and FirebaseMessagingService. My problem is when I send message from firebase console it invokes GCM receiver as well as Firebase notification.

KiDa-2049 avatar Sep 14 '16 10:09 KiDa-2049

thank you @KishorAndroid , i will try it

abdymm avatar Sep 15 '16 02:09 abdymm

@abdymalikmulky I have same issue ...When you can solved then please Tell Us!!

and How to set BigPictureStyle() in FCM notification when app is in background or Destroyed!!

nikunjparadva avatar Sep 16 '16 12:09 nikunjparadva

Why is this issue closed? Since the main issue was about "Custom Notifications when app is in background or killed" and not solved yet? Does this mean we should not wait for a fix to this? I was really hoping Firebase team to be working on this.

yayaa avatar Sep 19 '16 20:09 yayaa

@yayaa I think you need to read all the thread first. It's definitely not a bug and everything is working as planned

Svoka avatar Sep 19 '16 20:09 Svoka

I know it is as planned, but i thought at least this would be considered as feature demand. Most of the applications icon's somehow has a background, and when firebase applies white tint on it to display as notification icon, it is mostly white squared empty icon or some non-sense icon there. At least, customisation on icon - color and so on. I see on notification message documentation there are some fields like that and i thought maybe it's already done and i missed that, but when i try to send that from console as additional params in advance section, i saw no changes. That's why i actually said no improvements on this topic. @Svoka but thanks anyway.

yayaa avatar Sep 19 '16 20:09 yayaa

@yayaa oh, I think I get it now. In my case I don't need console at all, because we have server with this logic. So we send data-only messages. (Console still can't do this, I guess) By the way, you can provide icon from console: it's R.drawable.#_ICON_STRING#

Svoka avatar Sep 19 '16 20:09 Svoka

I know with data messages you can do anything. But we talked about having notification messages sent from console would also be customizable. And then i saw that issue was closed... :(

yayaa avatar Sep 19 '16 20:09 yayaa

@yayaa I guess we need to create separate issue for that :) In context of possibility to show icon this issue is closed.

Create new issue https://github.com/firebase/quickstart-android/issues/139

Svoka avatar Sep 19 '16 21:09 Svoka

@Svoka Can you confrm that you build your application with Android Studio 2 to receive data only messages to background application?

2ndGAB avatar Sep 20 '16 06:09 2ndGAB

@yayaa I don't think if I undertsand you well but if your are talking about icon appearing on status bar when you receive a notification, they are mandatory white on transparent background since Lollipop. This icon is set like that:

Notification.Builder..setSmallIcon(android.R.drawable.my little_icon)

2ndGAB avatar Sep 20 '16 06:09 2ndGAB

Yes, i am aware of that @2ndGAB . But the problem here is when you have an icon with some graph on a solid background, and when firebase automatically creates your notification icon by tinting your application icon as white, it ends up like @abdymalikmulky screenshot.

Anyway since this issue is closed, i will just stop watching until i need to use Firebase again, and just hope them to fix it by then.

yayaa avatar Sep 20 '16 06:09 yayaa

@yayaa That's very strange, here is how I create my notification from a data only message, received in onMessageReceived()

        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.launcher_icon);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_logo_notif)
                .setLargeIcon(bm)
                .setContentTitle(title)
                .setSound(ringURI)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg).setSummaryText(this.getString(R.string.label_service)))
                .setContentText(msg)
                .setContentIntent(contentIntent)
                .addAction(R.drawable.ic_action_place, null, geoPendingIntent)
                .addAction(R.drawable.ic_action_web_site, null, webPendingIntent)
                ;

And here is the result: first the litle monochrome icon white on transparent background And here is when you open the notification center

You can see that if you provide a valid little icon with setSmallIcon(), you won't have a white square in the status bar, same if you provide a valid large icon with setLargeIcon().

I usually build my icons with Android Asset Studio

2ndGAB avatar Sep 20 '16 09:09 2ndGAB

I want to send a image in push message using firebase consol how can i send image in push message any solution.and also i want to custom look of notification..?

jaydip5500 avatar Sep 21 '16 10:09 jaydip5500

I want to send a image in push message using firebase consol how can i send image in push message any solution.and also i want to custom look of notification..?

jaydip5500 avatar Sep 21 '16 10:09 jaydip5500

I send User segment push notification but All user don't get push notification Why all did not get? From console.firebase.google.com I found monthly active user 4100 but when i send notification It shows 2100 send. and every time i got notification in 7 to 10 phones out of 12 different phones. That mean 50% user found notification. then what would be be others.

tariqul000 avatar Sep 22 '16 09:09 tariqul000