Firebase notification won't default to `title_loc_key` if normal `title` is set
Asked Answered
M

2

7

I'm using com.google.firebase:firebase-messaging:20.0.0 on Android. When I try sending a notification like the one below it always shows the normal title and body instead of title_loc_key and body_loc_key when they are presented in the app! The fun thing is that it's not the case in iOS. In iOS it will always try to first show the localized resources and if it couldn't find them, it will default to normal ones.

{
 "to" : "f6_numko7IQ:APA91bFrTN0fmThFDeAFy2...",
 "collapse_key" : "type_a",
 "notification" : {
     "title_loc_key": "resource_name_1",
     "body_loc_key": "resource_name_2",
     "title": "Default Title",
     "body": "Default Body!"
    }
}

Is this a known issue? Is there a get around for it?

Marieann answered 25/9, 2019 at 13:24 Comment(5)
Have you found a solution ?Purpure
No not really! I tried updating the version, but nothing. The worst part is that it works for iOS like a charmMarieann
any news about that ?Wedged
@Fatimaayaa not really.Marieann
@Reza Bigdeli i tested when the app is in background , is work fine , but in Foreground is always shows the normal titleWedged
I
1

The solution is to remove the "title" and "body" entries. I know it should work like on iOS but currently it's not.

{
 "to" : "f6_numko7IQ:APA91bFrTN0fmThFDeAFy2...",
 "collapse_key" : "type_a",
 "notification" : {
 "title_loc_key": "resource_name_1",
 "body_loc_key": "resource_name_2",
}

}

Isolt answered 26/11, 2021 at 10:38 Comment(4)
Thanks for respond, Android is behaving differently. But with your solution we won't have a default title, not even on iOS.Marieann
What you mean by default? If there is no key in the app? Then there is nothing to be presented? You can implement the default in the app itself. Use a key in default strings folder. And then specific language values in language folders. For ex DE and FR. This way when someone has a FR smarphone FR string is shown, if anyone has DE language DE string is shown, all other languages will show the default (English for example.)Isolt
Let's say we want to introduce a new push notification. And the users have not updated their app and therefore will not have any localization key for the new notification in their language files. In this scenario, we would like Android to show title and body of the message. But for the users who updated their app, we would want to show the localized string. This scenario cannot be handled if we remove title and body entirely.Reece
Did you ever figure out a solution for the use case where a user hasn't updated their app yet and a new notification is added?Wolford
A
0

For Android: Using the "v1" api from firebase remove "title" and "body" from root "notification: {....}" and add into the "android": { "notification": { "title_loc_key": "push_notification_title", "body_loc_key": "push_notification_body" } },", should work.

But you need to keep in mind the following:

  1. The push notification will be displayed by default by firebase SDK based on the com.google.firebase.MESSAGING_EVENT, when the app is in the background --> opened (the process is in the background) and Notification permission is allowed. Also, you need to be sure that your app has already a channel created with the default_channel_id you defined in the manifest file. For this case, you will not be notified of the onMessageReceived callback provided by "FirebaseMessagingService".

  2. When the app is in the foreground you will not receive any firebase notification, visible on the screen. Based on the Channel you have created at point 1) you need to trigger the notification display if required, by creating a NotificationBuilder .... and displaying it. This catch must happen inside the: onMessageReceived callback provided by "FirebaseMessagingService"

  3. When the app is closed and notification is allowed the push notification will be displayed by default by firebase SDK based on the com.google.firebase.MESSAGING_EVENT. However, if the notification received by the device contains some data: {} payload that can be handled into the onCreate lifecycle method from activity by accessing the this.intent.extras. You will not be notified of the onMessageReceived callback provided by "FirebaseMessagingService".

  4. If notification is disabled you will not get any push notification displayed visually but when the app is in the foreground you will be notified into the onMessageReceived callback provided by "FirebaseMessagingService". When the app is closed or in the background, you will get nothing.

Araujo answered 9/2, 2023 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.