FCM Server side differentiate between iOS and Android - 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

Related

Firebase notifications on ios - React native

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.

Send data to Android device Notification to IOS devices

We are using firebase to send notifications to both Android and IOS devices from server and we are using legacy FCM to send notifications.
But when our app is backgrounded notification is handled by system itself and thus we couldn't handle it by app. So we need to add data to notification to handle the notification by application itself on android side.
But due to we also have ios devices we couldn't remove notification from the pattern.
Is there any way to send data to Android devices and notification to IOS devices?
Also you may find the pattern that I mentioned at below.
{
{
"registration_ids":[
],
"content_available":true,
"priority":"high",
"data":{
"title":"",
"text":"",
"badge": 1,
"sound": "default"
},
"notification":{
"title":"",
"body":"",
"badge": 1,
"sound": "default"
}
}
You will have to map the FCM Registration ID with the OS Type and store it somehow. Now when you are sending the notifications, based on the OS Type, you will change the body structure for the notifications.

How to send notifications with a phone / Cordova mobile app

I'm new to Cordova and JS stuff, and currently making a Cordova mobile app. The purpose is to send notifications with the help of firebase (I'm focusing on android for now).
The notification has to be send with a phone, to an other phone.
I use JQuery Mobile 1.4.5, JQuery 2.1.0 and Cordova CLI 6.1.1
My project is a Visual Studio project (techno restriction)
I'm using this Cordova FCM plugin: https://github.com/ostownsville/cordova-plugin-fcm
My code to receive the notifications works pretty well: No errors, token generated successfully, notification received ! (notifications sended by firebase console, with the token, etc.).
==> My problem here is that I don't know how to proceed to send notifications to an other phone !
I already have a payload example, that can be use to send the notification. but I don't know how to use it. I'm sure I have to put it in firebase, somewhere, but i don't know where.
Plus, I can't figure how to obtain the token of the phone that I want to send a notification. Is it possible to know the receiver token ?
here is my payload code:
var fcm_server_key = "aiz******************************";
method: "post",
datatype: 'json',
headers: { 'content-type': 'application/json', 'authorization': 'key=' + fcm_server_key },
url: "https://fcm.googleapis.com/fcm/send",
data: json.stringify(
{
"notification": {
"title": "title ", //any value
"body": "body ", //any value
"sound": "default", //if you want notification sound
"click_action": "fcm_plugin_activity", //must be present for android
"icon": "fcm_push_icon" //white icon android resource
},
"data": {
"param1": "value1", //any data to be retrieved in the notification callback
"param2": "value2"
},
"to": "<USER_TOKEN>", //topic or device ==> maybe the place to put the token !?
"priority": "high", //if not set, notification won't be delivered on completely closed ios app
"restricted_package_name": "" //optional. set for application filtering
}
)
==> Is it possible to send notifications with a phone to an other phone, with firebase ? how then :) ?
Finally, I use the the Cordova Contact plugin to list all the contacts in the phone. Is there an other way to show to the user his contacts and then send the notification to the right person ?
Thanks a lot for your help. I've already checked the stackoverflow forum and the plugin issues section in order to solve that problem but I haven't find the solution yet.
(Sorry for bad english, this is my first post...)
To answer to your question there are more levels
My problem here is that I don't know how to proceed to send
notifications to an other phone !
Push notification can be sent through firebase console.
Is it possible to know the receiver token ?
Yes if you Initialize firebase plugin, you will get the firebase token in your javascript function inside FCMPlugin.getToken()
Is it possible to send notifications with a phone to an other phone,
with firebase ?
You can send only from one receiver to another receiver directly using firebase push data. But I wont recommend you that. Because it is not safe to exchange data directly between receivers without a server
Note:
Before starting firebase notification I would like to give you a note. There are 2 types of payloads in firebase.
1.Notification payload : This has a few limitation in receiving notification that, the app will get notification only when app is in background. So notification will not be shown to receiver if he is using the app. It will be handled by your FCMPlugin
1.Data payload : If you need to show the notification even if the app is in foreground then go for it. But you need to trigger the notification manually in your java code in android way

How to get the notification title and body using cordova-plugin-fcm on an ionic 1 app

I am using Firebase Cloud Messaging HTTP Protocol to send push notifications to my application using postman for testing.
I am using the following code to send the push.
{
"notification":{
"title":"Title",
"body":"this is a notification to a specific topic",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
},
"data":{
"action":"ping"
},
"to":"/topics/Topic_1",
"priority":"high"
}
and I am using this code to handle the notification on my app:
FCMPlugin.onNotification(function(data){
console.log(data);
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert('notification tapped'+ JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert('application is open'+ JSON.stringify(data) );
}
});
it is all working except i cannot get the title and the body to use them in my application, all I am getting is the following:
Object {wasTapped: false, action: "ping"}
I cannot find a way to get the notification title and body.
I know I can copy them to the data section but that's not logical its a dirty workaround
so any idea how to get the notification data?
thank you.
Basing from the behavior shown, I'm presuming that the client platform is Android. If so, then this is working as expected.
When sending a combination of both notification and data in your message payload, the Android System tray will be the one to handle the values in notification. The dirty workaround you mentioned is the only workaround so far -- a workaround that I actually suggested on one of my answers as well.

Data payload not being handled on Android when using `content_available`

I'm trying to send a payload using GCM that can be handled on the background or foreground by clients using Android and iOS. The messages are delivered to clients using both platforms that are subscribed in a topic.
Because I need the iOS app to handle the data even when the app is on the background or closed, I'm setting "content_available": true, which works as expected on iOS.
However, when I add that parameter, Android does not handle the received data and instead displays a blank notification.
Here's an example payload:
{
"to": "/topics/946",
"content_available": true,
"data": {
"time": "2016-02-04 18:33:08",
"message": "Hello",
"msgType": "chat",
"senderId": "50",
"senderName": "John"
}
}
The documentation says that data messages should wake the Android app anyway:
On iOS, use this field to represent content-available in the APNS
payload. When a notification or message is sent and this is set to
true, an inactive client app is awoken. On Android, data messages wake
the app by default. On Chrome, currently not supported.
What am I doing wrong?
EDIT: I've already tried using "priority":"high", which does not make any difference.

Categories

Resources