Firebase cloud message to ios and android devices - android

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.

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.

Firebase Messaging not Working in Modern Android Instant App - DisplayNotificationRequired?

I've read all the related SO questions, most of which were asked in answered in 2017 or early 2018, before Google simplified the way Instant Apps could be created. In my case, I created an "instant enabled app bundle" (described here) that works both as an app and as an instant app.
The app bundle includes a library I wrote that is configured to receive Firebase messages (described here) from the AWS Simple Notification Service (SNS). The problem is that messages are received when the app is run, but not received when the instant app is run.
The good news is that when I look at the AWS CloudWatch console, I can see every failed attempt. Here is the relevant part of the message:
"providerResponse": "{\"results\":[{\"error\":\"InvalidParameters: DisplayNotificationRequired\"}],\"multicast_id\":\"8198293557962051\",\"success\":0,\"failure\":1,\"canonical_ids\":0}"
The message content is:
{
"to" : "fi_Pclw7RrWtPm0xMVSgbC:APA91bGJFzM6RQVisO0N_JOAb8rUOKBVPZ0I5jh9Vf-4f-xXtbQY_Ik7q3wLGeCbR5bh_lFWDy0PX-F2mIlamMlCTIuEqEOlk0KcFO9a5fYk6B_omGqevjY6KNiByI5j_vKQaF17Rif8",
"data" : {
"body" : "Content message",
"title": "the Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2"
}
}
I've searched for the DisplayNotificationRequired error but can't find anything. Anyone know what this means and how to fix? Thanks!
Since I wrote the above, I tried adding a notification object as well:
{
"to" : "fi_Pclw7RrWtPm0xMVSgbC:APA91bGJFzM6RQVisO0N_JOAb8rUOKBVPZ0I5jh9Vf-4f-xXtbQY_Ik7q3wLGeCbR5bh_lFWDy0PX-F2mIlamMlCTIuEqEOlk0KcFO9a5fYk6B_omGqevjY6KNiByI5j_vKQaF17Rif8",
"notification" : {
"body" : "Content message",
"title": "the Title"
},
"data" : {
"body" : "Content message",
"title": "the Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2"
}
}
Now I'm getting an error with MissingDataUri instead of a DisplayNotificationRequired error. When I use Postman to send this message directly to the device token, I also get a MissingDataUri error, so I'm thinking this is not a AWS SNS or a Firebase Cloud Messaging issue, but just an instant app issue.
Finally, I understand that there was an "instant app notifications beta" way back in 2018 that appears to be still running: https://g.co/instantapps/notifications
Is it still not possible to send a push notification to an instant app, specifically with a 'data' payload? (I went ahead an submitted the form...just in case.)
Thanks all!
The only way you can do it is with a notification attached to a foreground service.
Remote notifications are currently not possible with instant apps.
Can I ask why you want to do it? An instant app should deliver an instant experience and you should assume that the user will either install the full version, or abandon it after closing the app. So remote notifications does not make much sense, unless it is attached to the "experience" the user is expecting.
If a notification flow is necessary for the experience, you should use a foreground service. A good example could be if you buy a drop-in service that has a queue (hairdresser for example) and you want to notify the user during the waiting time.
In this case you could create a foreground service with an attached notification. The service polls your backend in the background to get a time estimate and queue position, and then update the notification accordingly. When the user is finished with his haircut, you can close the foreground service and thus allowing the app to be removed from the device automatically.
Read more about services: https://developer.android.com/guide/components/services
You will need: https://developer.android.com/reference/android/Manifest.permission#INSTANT_APP_FOREGROUND_SERVICE

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

Is aps dictionary necessary in iOS notification payload? Can we have same payload for iOS and Android?

I just started working on notifications on iOS and it seems that apple has defined a format for payload to receive notifications.
So currently, I am using this payload and everything is working as expected. I am getting title, subtitle, body sound, image.
{"aps" : {
"alert" : {
"title" : "Introduction To Notification",
"subtitle" : "Session 707",
"body" : "New Notification Look Amazing"
},
"sound" : "default",
"category" : "message",
"badge" : 1,
"mutable-content": 1
},
"attachment-url": "https://pusher.com/static_logos/320x320.png"
}
Lets say I want to have a single payload for both Android and iOS.
Is there a standard format defined in Android for notifications or can you set any data in Android and the client has to manually handle and display these notifications?
How can I create a payload which works for both?
Update for cross-platform payloads: A recent feature was added for FCM that gives an option to provide specific params for specific platforms, called Platform Overrides.
The sample payload you posted seems to be in-line with the official parameters for APNs. When using GCM or FCM, the parameters to be used are different (see the links).
Is there a standard format defined in Android for notifications or can you set any data in Android and the client has to manually handle and display these notifications?
It depends on which type of message payload you're planning to use. There are 2 types of Messages for GCM/FCM, notification and data.
notification messages only have predefined set of parameters available, while data messages can be used to have custom key-value pairs. Both are usually handled by the client, but note that the behavior for Android and iOS are different depending on the message type you use (see the links).
How can I create a payload which works for both?
As I mentioned in comments section in the other post:
You'll have to do the mapping in your own database/app server. Yes. What I was thinking here was every time a registration token is generated on the client app side, you send it to your database/app server along the type of device (i.e. "Android", "iOS"). So that when you'll be sending messages, you'll first have to check the type of device. I did say it's more work, but it's a sure way to give you control over things. AFAIK, it is the developer's responsibility to keep track of the registration tokens and any details that should be associated with it.
You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:
Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.
So in your case you should do something like:
{
"aps": {
"alert": "Hello World",
"sound": "default"
},
"Person": {
"Address": "Your address",
"Name": "Your Name",
"Number": "XXXXXXXXXX"
}
}
Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":

Contextual push messages and badge icon in FCM

Up until now I have been using APNS and manually connection to their services over TCP. I have packaged and delivered all push notifications in a single operation by writing all of the combined data to APNS.
Things are moving along and Android is starting to become relevant, so I looked into FCM as a common platform for push.
I cannot figure out how I am supposed to send, let's say:
"Hello (username)! Blah Blah blah blah whatever"
... to many users at one.
The FCM docs allow me to specify multiple device tokens, which is great, but they will all receive the same notification. Is there no way to customize this per device/token? Also this is a big problem when it comes to the notification badge count in iOS. If everyone receives the same notification payload, they will also receive the same badge icon count.
Am I really to believe that I am supposed to make an HTTP request for every single user to make this work properly across both iOS and Android?
{
"registration_ids" : ["token1", "token2", "token3"], <--- devices
"priority" : "normal",
"notification" : {
"body" : "Hello, Maria! Whatever blah blah", <-- Same message
"title" : "Whatever",
"badge" : "3" <--- all users receive the same? How?
}
}
I tried posting the payload as an array of above objects in a single request, but that does not work (as expected, since it's not documented).
How do we solve this?

Categories

Resources