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

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

kroikie avatar Jun 01 '16 20:06 kroikie

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:[email protected]] Verzonden: woensdag 1 juni 2016 22:26 Aan: firebase/quickstart-android [email protected] CC: NL-Splinter [email protected]; Mention [email protected] 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 .

SplinterII avatar Jun 01 '16 21:06 SplinterII

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.

pinkeshdarji avatar Jun 02 '16 11:06 pinkeshdarji

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));
        }

SplinterII avatar Jun 02 '16 16:06 SplinterII

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

swatijiet avatar Jun 03 '16 02:06 swatijiet

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

Svoka avatar Jun 03 '16 07:06 Svoka

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?

gauravjain131 avatar Jun 03 '16 10:06 gauravjain131

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

seyfullahbilgin avatar Jun 04 '16 08:06 seyfullahbilgin

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?

martinodecarlo avatar Jun 04 '16 12:06 martinodecarlo

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.

xo0ps avatar Jun 12 '16 11:06 xo0ps

Can any1 guild me how to send multiple push notification via push API???

nvhaiwork avatar Jun 29 '16 04:06 nvhaiwork

Hello ,

some time ago published in

http://stackoverflow.com/questions/38020348/send-a-notification-background-firebase-android

a problem that I have with the push notification in foreground and backgrand FCM after you read the link sugueris me to look at my app android?

backgrand foreground and take different style.

Regards,

diegogui1 avatar Jul 02 '16 14:07 diegogui1

Can anyone please provide some example how can i use the api to send data only messages (php), currently i am using console to send notification and data, but it acts differently when app is in background or foreground..

ghost avatar Jul 11 '16 01:07 ghost

I have the same question. Is there any simple way to send the data only messages?

brianhsux avatar Jul 14 '16 10:07 brianhsux

@xainbutt it's really simple, you can adapt the following code:

`$toWhom = ''; $serverKey = ''; $serverFrom = '';

// The data to send to the API $postData = array( 'to' => $toWhom, "notification"=>array( 'title'=>"Test", 'body'=>"Test2", "icon"=>"ic_stat_ic_notification" ) );

// Setup cURL $ch = curl_init('https://fcm.googleapis.com/fcm/send'); curl_setopt_array($ch, array( CURLOPT_POST => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_SSL_VERIFYPEER=>false, CURLOPT_HTTPHEADER => array( 'Authorization:key='.$serverKey, 'Content-Type: application/json' ), CURLOPT_POSTFIELDS => json_encode($postData) ));

// Send the request $response = curl_exec($ch);

// Check for errors if($response === FALSE){ die(curl_error($ch)); }

print '

' . print_r($response, true) . '
';`

Svoka avatar Jul 14 '16 11:07 Svoka

Just want to leave my feedback about the notification framework: We as developers should have full control of the notifications. I never wanted FCM to auto-notify my system bar for me and i should ALWAYS receive the "onMessageReceived" callback, doesn't matter in foreground or background.

Pehaps we could have a checkbox on the Firebase Notification console indicating "Auto-notify". If unchecked, we get the "onMessageReceived" callback. Otherwise, it notifies the system bar.

shinayser avatar Jul 14 '16 15:07 shinayser

Agreed

On Thu, Jul 14, 2016, 18:43 shinayser [email protected] wrote:

Just want to leave my feedback about the notification framework: We as developers should have full control of the notifications. I never wanted FCM to auto-notify my system bar for me and i should ALWAYS receive the "onMessageReceived" callback, doesn't matter in foreground or background.

Pehaps we could have a checkbox on the Firebase Notification console indicating "Auto-notify". If unchecked, we get the "onMessageReceived" callback. Otherwise, it notifies the system bar.

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

Have a nice day Yishai Levenglick Android developer +972-54-520-9569

yishai-glide avatar Jul 14 '16 15:07 yishai-glide

@shinayser I think you just misunderstood how android notifications is working. This is the CORRECT behavior

Svoka avatar Jul 14 '16 16:07 Svoka

@Svoka No, I did not. When in background, the Firebase Notifications (in Console) doesn't have data-only notifications. So, we can't customize it when app is in background.

shinayser avatar Jul 14 '16 21:07 shinayser

@shinayser Why don't just use CURL or something instead? They'll be done with the console and data notifications soon.

Svoka avatar Jul 14 '16 21:07 Svoka