Sending silient data message to an Android app - android

I am very new to android app development and am wondering if there is a way to silently push a data message to a user's phone.
Basically, I have managed to set up Firebase notification on my app, and the app is able to receive notifications when the app is not running in the foreground. However, I want to be able to update some data in the user's phone without the user seeing a notification. Is this doable?
Thanks!!!

yes it is doable.You can send data by calling your api in async task without informing the user and update the data in application without informing the user.
But this will happen only when the application is in foreground.
2nd method is to use service and update your data even after the app is not in foreground.
I will personally prefer to use service.

To do that you need to send the message payload with data payload instead of notification to the firbase from your server where the notification payloads get generated.
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
So that you will receive the message in the Firebase service class inside onMessageReceived(). Once the data received then using key values you can get the data.
Refer the link to know more
https://firebase.google.com/docs/cloud-messaging/concept-options#data_messages

Related

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

Send specific push notification messages to different users in a single request to GCM

i want to know how we can send multiple user's specific push notifications messages in a row.
For example using GCM, if you want to send a specific message to a specific user you can use this content in your POST request to Google servers
{
"to" : "user_registration_id",
"priority" : "normal",
"notification" : {
"body" : "You have 3 messages in your inbox",
"title" : "NewsMagazine.com",
"icon" : "new",
}
}
The problem that i'am facing right now, is that i have tons of notifications to send at a time, each notification is user specific, so i have to make multiple POST requests in a row to Google servers to send each notification separately, and this cause a huge performance issue on my server. What i want is to be able to send them in a single POST request
Any idea on how this might be implemented ?
Thank you.

Android silent push notifications

We think that Android’s push notifications by default are silent notifications and must be the developer who programmatically raise the notification on the screen. Or the push notifications aren’t silent by default?
Today we work using this JSON structure which our app receives from the GCM server.
{
"data":
{
“Type” : “2”,
“_dId” : “3718829”,
“_mId” : “9924012”,
“_msg” : “HOLA JVE”,
“collapse_key” : “9924012”
},
"to" : "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Is this structure valid for silent notifications or there is a different one ?
Truly we are pretty lost on this topic. Following this question, anyone have a json structure of an Android push notification with message and title tags ?
Other question : When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
Or the push notifications aren’t silent by default?
There are two ways that a notification is received in Android, either it's Notification Tray, or you handle it yourself in onMessageReceived() depending on which payload you use (Notification or Data).
Basing from your inquiry (silent push notification), I'm guessing you'd prefer the latter, since you'll be able to handle it yourself. As per the GCM Payload docs:
Use notifications when you want GCM to handle displaying a notification on your client app’s behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app,..
So what you are currently using right now, (a data payload) should be fine. However, if you have both notification and data in your payload, you will have to consider your app's status. Referring to this FCM docs, for data payload:
App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.
..anyone have a json structure of an Android push notification with message and title tags ?
As also mentioned in the docs I linked above:
Data messages have only custom key/value pairs.
So I think it's safe for you to just use keys so long as it's not a reserved word, as mentioned in this docs:
The key should not be a reserved word ("from" or any word starting with "google" or "gcm"). Do not use any of the words defined in this table (such as collapse_key).
When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
I think what I mentioned above pretty much covers this part (see the table).
On Android, differently from iOS, the app is responsible for creating and showing the push notification. So yes, you can think of them as silent by default, although you don't exactly have this concept on Android.
What I normally do is, if the notification should not be shown, add a silent field. For example:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "Will not show this message",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
"silent": "true"
}
}
And then you can use the silent field to show or not show the notification according to an if statement.

Handle the data payload without user tapping on the notification?

When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
Is there any way to handle the data payload without user tapping on the notification?
Basing from the FCM docs on Handling Messages:
App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.
It's not specifically handled ONLY by tapping on the Notification. You may handle it in the onMessageReceived(). Pretty sure the tap action also depends on how you implement it.
If you intend for it to do something else, do provide more details.
If you want to receive data payload when app is not running then you need to use a custom app-server with http protocol(in your case) which will send only data payload no notification and will send data to fcm endpoint like this
{ "data": { "some_key" : "some_value",
"some_more_key" : "some_more_value" },
"registration_ids": ["device-token","device-token2"] }
or just to send data from server to one client
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Test",
"body" : "Your message here",
"Room" : "Some Test Room"
},
}
Then using POST you can send downstream message to your device and it will work the way you want. If you want to have a basic idea of such app server please consider this : How to push notification to client when Firebase has a new entry?
I have posted basic Java Servlet code here and also a node.js code by firebase official blog.
Hope it helps.

Categories

Resources