Custom Notifications when app is in background or killed
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.
It's been trending topics about Firebase Cloud Messaging that Data Payload message is not fired onMessageReceived() method for some lower end device. And it's true. Here is the result.
App is killed scenario
**Devices got payload:: 1. Nexus 5s : OS 7.1
Device not get payload:: 1. Xiomi MI 4c: OS 5.1 2. Huawei LUA -U22 :OS 5.1**
Ensure that i'm not sending notification key from server / postman.
I am looking for the solution, i got data payload from firebase including all types of devices and Android OS & obviously From API level 15. My postman details are as follows.
{
"registration_ids": ["fC5uxGSRCsg:APA91bHh9fMXQ41LpX6tjjSsBKGrKTWYpzKimLDzvBGSHDPo2pq87JHqogUp2kqrmJi06siG_p6DfgRCim23iFzlBQAIrgtMDqRW4s39zUqv9CzyPqzxVl5PtnHPRDs4OagTuTePNyDI"],
"data": {
"title" : "my_custom_value",
"message" : "tekksdasdasdsa",
"isBackground" : "",
"payload" : {
},
"timestamp" : "",
"imageUrl" : "",
}
}
the only way to resolve this issue is to use the handleIntent method. This function will always get called. or we can go back to GCM
@Override
public void handleIntent(Intent intent) {
// super.handleIntent(intent);
Logger.d(intent);
Logger.d(intent.getExtras().getString("key"));
Logger.d(intent.getExtras().getString("gcm.notification.body"));
Logger.d(intent.getExtras().getString("stuff"));
if(intent.getExtras()!= null){
notification_msg = intent.getExtras().getString("gcm.notification.body");
notification(notification_msg);
}
}
In FCM, you can no more send silent/Custom push notification to the apps running in Background by using console. You have to use POST request to send them.
The details can be found out here https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol
PS: Make sure for sending custom push notification, you are having just 2 keys in body - data and to
What about showing image from url ? I want to add imageUrl when sending notification from Firebase Functions. Something like:
const payload = {
notification: {
title: `xx`,
body: 'xx,
icon: 'imageURL',
},
};
return admin.messaging().sendToDevice(tokens, payload);
Notification is received when app is in background.
how about my solution https://stackoverflow.com/questions/48897883/how-to-handle-notifications-with-fcm-when-app-is-in-either-foreground-or-backgro/48899186#48899186
Agree with @jemshit. It was really convenient to specify a link for icon in notification push. Are there any plans to add this?
Thank you.
@pavelshackih @jemshit thanks for that feedback, the team is looking into this type of feature.
I'm still having issues with firebase push notification. It only customizes my push notification when the app is in the foreground.
Hi! I need to somehow remove extras from an intent that was delivered by FCM.
- App is closed
- I send push notification from the fcm
- When a user clicks on notification tray icon app starts and retrieves data from intent's extra
- Then I want to clear these extras to prevent unexpected behavior in the future (I use intent.removeExtras())
- After that, I click the back button (an app is closed), then through task manager I choose my app
- An app is started and intent still has extras from fcm in it.
Question: how I can completely remove given extras?
@BulatMukhutdinov pressing the back button does not close the app. Your activity might be resumed with the same intent as the previous one. Maybe if you swipe your app away from the recents screen then you would have your app launch with an intent with empty extras. I would suggest having some type of check to see if the extras have already been handled so you can ignore them if so.
@kroikie yes, an app doesn't close, but the activity is recreated. Although I remove extras, the recreated activity still receives these extras. Is it intended behavior? How can I completely remove extras? Sure, I can store somewhere if I have already handled extras but it seems to me as a workaround
@BulatMukhutdinov I was able to reproduce this. I believe the recents menu reuses the intent last used by the activity. As you have seen, I don't think removing the extras from the intent in the activity has an effect on the intent used to recreate the activity from recents menu.
Maybe in a future release we could add controls that will allow the developer to define how the launched activity is handled by the recents menu but for now you will have to handle those intent extras yourself.
Why does not SDK call onMessageReceived in background? I can not find a reasonable reason.
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 ?
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.
i has know how to handle this issue.
Mehdi Dehghani [email protected] 于2019年6月12日周三 下午2:24写道:
Why this issue is closed?!
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-android/issues/4?email_source=notifications&email_token=ABMS2DPHR7OWLM63HJ5A5DLP2CJCTA5CNFSM4CEJOTS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXPMBVI#issuecomment-501137621, or mute the thread https://github.com/notifications/unsubscribe-auth/ABMS2DOCUA2KGYXUYGOOSCDP2CJCTANCNFSM4CEJOTSQ .
@flyme2012 thank you, actually I found the solution. of course after posting my prev comment.
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
https://github.com/firebase/quickstart-android/blob/375c1ae5ec9000ee71b93cee409086e27d774bdb/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt#L43
Creating notification function (open the link to see sendNotification function) https://github.com/firebase/quickstart-android/blob/375c1ae5ec9000ee71b93cee409086e27d774bdb/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/kotlin/MyFirebaseMessagingService.kt#L114
hey @ckarthikv actually i am facing a issue in IOS when the app is in background. #608 Can you please have look into it,.
git clone https://gerrit.googlesource.com/gitiles