Firebase notifications on ios - React native - android

I'm trying to get push notifications running with React Native on iOS and Android.
To do so I am using firebase, as that seemed to be very convenient.
On Android I managed to show notifications when the app is in the foreground and at least show a log when the app is in the background, meaning I can process them.
On iOS I have not managed to do any of those, not from our custom backend in aws (via SNS), nor from firebase console with fcm token. What interestingly worked, was to show a badge on the ios app icon when sending broadcast messages to all users. So something seems to be received on the phone.
Now looking into this a little deeper I found that iOS needs notifications in the following format:
{
"Simulator Target Bundle": "com.compass.SomeExampleApp",
"aps": {
"badge": 0,
"alert": {
"title": "Push Notification Test",
"subtitle": "Hey! 👋",
"body": "Is this working?",
},
"sound":"default"
}
}
At least this is how you can test them on the sim.
1. Does it mean that I have to send the notifications in that same format? Or can it be something like:
{
"data":{},
"apns": {
"Simulator Target Bundle": "com.company.Example",
"aps": {
"alert": {
"body": "Wellcome to MyApp!",
"title": "MyApp"
}
}
}}
2. How can I send a format that iOS AND Android can read, show on screen and process in background if necessary?
Since we are sending only data messages I think this might be part of the issue, if I look at our backend.
Still when sending from firebase I am not able to show anything on screen on ios Device. Any hint?

I found a solution that actually works on both platforms although it is a bit weird since it seems to be outdated. Code needs to look like this to send to both iOS and Android:
{ "GCM": "{ \"notification\" : {\"content_available\" : true }, \"data\": { \"body\": \"Sample message for iOS endpoints\", \"title\":\"Hello world\"} }" }
As seen here APNS attributes seem to at least partly work if added within the GCM message like content_available in this case or badge.

Related

How should I send notifications with push notifications thru FCM for both Android and iOS?

I am trying to send notifications to both Android and iOS devices using FCM. The front end was coded with Flutter. I am working with Typescript/NestJS at the back end. For some reason, iOS devices receive all the notifications with a push notification/pop-up but Android devices sometimes don't. Strangely, the notifications ALL arrive at the test device INBOXES. It's just that sometimes, Android devices do not show a pop-up/push notification.
Targeting users thru a Firebase topic works as expected: push notifications do arrive for Android and iOS. I do have to find and filter out the users to target before sending the target device IDs to FCM. I am confident my filters are correct. The notifications arrive in the app inbox for both Android and iOS after my filtering. Also, changing the device ID/token in my database causes an error from FCM:
{"code":"messaging/registration-token-not-registered","message":"Requested entity was not found."}
If I revert the data to its original values, the app runs fine. I've checked and compared the data I pass to Node.js' FCM module and it looks fine. Here, messaging is an instance of firebaseAdmin.messaging().
param = {
"android": {
"data": {
"id": "67bcd478-f643-4c9e-848a-be354a6b85d4",
"icon": "default",
"title": "Test",
"body": "Direct From Playground"
}
},
"apns": {
"payload": {
"aps": {
"id": "67bcd478-f643-4c9e-848a-be354a6b85d4",
"icon": "default",
"title": "Announcements",
"body": "Direct From Playground",
"alert": {
"title": "Announcements",
"body": "Direct From Playground"
}
}
}
}
}
messaging.sendMulticast({
tokens: value as string[],
...param,
});
Again, the code above sometimes successfully triggers push/pop-up notifications on Android devices but ALWAYS on iOS. Could this be a problem for the front-end handling of my data? Am I missing some parameters?

FCM Server side differentiate between iOS and Android

I am sending push notifications from my server (to a firebase token and not to a topic). I do not differentiate between the platforms, the server just sends the JSON via RestTemplate (Spring Boot App).
If I prepare it for Android, it works, but it does not for for iOS. But if I prepare it for iOS, it does not work for Android.
The case is: I want the notification and the background process (It should display the notification in any case foreground/background/killed and it should take key-value pairs from the data payload).
The problem is: If I use notification in my JSON, the notification would be displayed on iOS and on Android but just the iOS can handle the payload (in foreground/background/killed state). If I remove the notification tag in JSON, then everything works fine (Android and iOS receives the payload in all states, but the iOS does not display the notification).
In Android you can build the notification client side (like me), but in iOS its not possible.
So, how can I send a FCM (JSON) with a firebase-token and it is platform-independent? Both can handle it properly, both receive the payload and displays the message and payload should be handled in every state.
This works on android and displays the message (built client side) but does not work for iOS
{
"content_available": true,
"data":{
"action":"bla",
"value":"true",
"id":"111111"
},
"android":{
"priority":"high"
},
"apns":{
"headers":{
"apns-priority":"10"
}
},
"to":"<MY_FCM_TOKEN>"
}
This displays the FCM for android and iOS, but handles the payload just on iOS
{
"notification":{
"sound":"default",
"title":"Test Title",
"body":"Message of FCM"
},
"content_available": true,
"data":{
"action":"bla",
"value":"true",
"id":"111111"
},
"android":{
"priority":"high"
},
"apns":{
"headers":{
"apns-priority":"10"
}
},
"to":"<MY_FCM_TOKEN>"
}
Or is it possible to declare the notification-tag just for iOS?
Or is it possible to create notifications also client side in iOS when a payload receives?
Before sending a notification on the Server side I am checking whether the the platfomr is iOS or Android by using google's method:
https://iid.googleapis.com/iid/info/IID_TOKEN
Which on success returns a JSON with a 'platform' parameter.
You can find more info here

Google FCM does not play notification sound on Android Phone

I am working on a cordova app and using the cordova-fcm plugin to enable push notification on my app. Everything works find, the plugin can retrieve tokens, subscribe to channels and I can send notifications to my device using the FCM HTTP v1 API. The problem is the device does not play any sound! I have tried setting up the payload to "sound":"default" or a custom sound file put in the res/raw directory, but no luck. Now I looked into the source code of the above said plugin and found that onMessageReceived callback does not have the code to forward the notification using notificationManager.notify(). Now I have a confusion here. When I was reading FCM docs, what I understood is that when a message is received in the background or the app is killed, the message is handled by the system and I don't need to do anything with notificationManager.notify(). Do I understand it correct? Also the payload has a "sound" property and it is mentioned in the doc that either I can pass "default" or any custom file name I put in res/raw directory. So how is this handled, I mean which sound needs to be played? Is this handled by the system? Also if I implement notificationManager.notify(), then how do I do setSound() as per the "sound" property in the received payload?
Here is the payload that I have tried...
{
'message': {
"topic": "all",
'notification': {
'title': 'FCM Notification',
'body': 'Notification from FCM',
},
"android": {
"priority": "high",
"notification": {
"sound": "default",
"click_action": "FCM_PLUGIN_ACTIVITY"
}
}
}
}
Never mind. It seems like a problem with my phone. Sound is played on other devices.. I tried to find a setting on my phone to check if sound needs to be enabled for some particular app notifications, but I could not find it either. Just to let the the users know I am using MI Note 5 Pro. If someone else stumble on this issue, this might help.

Firebase cloud message to ios and android devices

I have an app with Android and iOS users, subscribed to Firebase Clould Messaging topics. Some users have access to send a message to these topics. I make an http request to send the message. The body looks like this:
{
"to": "/topics/debug"
"notification" : {
"body" : "Hi there",
"title" : "Wow!",
"sound": "default"
},
"priority": "high"
"data": {
"title": "Hi there",
"body": "Wow",
}
}
For android, in order to send data message, I need to use the "data" key, so that onMessageReceived is called.
However, ios needs the "notification" key, so that even if the app is killed the message will display. But, whenever I add the "notification" key, the android devices don't call onMessageReceived, and it isn't treated as a data message.
How can I solve this issue, to make one request which is work for both devices. If I do two requests (one with "notification" for ios, and one with "data" for android, the android devices get two notifications (one from onMessageReceived, and one from the system. Can I stop the android devices from showing the message if its sent with just the "notification" key?
I hope I explained the issue well, since this is how I understand the problem I have.
Thanks
I'm not sure this counts as an answer, but I realised an easy workaround, that I prescribed different topics to android and ios users, and set different code appropriate for each one, instead of having one topic for all users.

Correctly formatting GCM notifications?

I'm currently trying out the google cloud messaging service with its sample application "Guestbook." https://developers.google.com/cloud/samples/mbs/
I'm attempting to send notifications that should appear as a simple "hello" toast on screen for about 5 seconds.Doing this from the project settings page (pictured here)works.Perhaps i've misinterpreted what this for.
However my attempts to do it manually via fiddler2 cause the guestbook client application to crash instead of making the intended toast flash up onscreen.
Here is an example of the POST request i used to attempt a push notification
I have also done other variations of the above where i have included "topicId:_broadcast" to no avail.
Is there something in the formatting that i have wrong? or missing information i should have included.
You got the format a bit wrong. It should be :
{
"registration_ids":["xxx", "yyy"],
"data": {
"message": "test",
"duration": "5"
}
}

Categories

Resources