Huawei Push Kit in Android : Send notification message and data message - android

I am seeing how Huawei Push Kit works in Android. For this I am using postman to send the notifications, I am sending a notification message and a data message. Notification messages are seen in the notification area, but I can't get the data that I sent in the data message. I would like to know how to get them both when the app is in foreground and background.

You have to include "foreground_show": false to get the message "data" payload to get delivered to "onMessageReceived".. otherwise it will only be displayed in the notification area .. and you wont be able to read the payload..
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/android-basic-receivemsg-0000001087370610-V5
Read "Receiving a Notification Message" Title in the above doc

There's a difference between HMS Core Push Kit and FCM. Notification messages will be delivered to the system tray, and data messages will be delivered to the onMessageReceived method by default when using HMS Core Push Kit. So notification message title and body cannot be obtained.
If you want to pass data to your app when sending notification message, you can tap the notification message to trigger the corresponding action such as opening the app, a web page, or a specific page in the app. To obtain data with customized actions, see docs.
For the data message parameters, see this:
{
"validate_only": false,
"message": {
"data": "{'param1':'value1','param2':'value2'}",
"token": [
"pushtoken1",
"pushtoken2"
]
}
}
and get the data with the onMessageReceived() method.

When you send a push notification without explicitly setting the value of the *message.android.notification.foreground_show = **false***, the data content won't be read because the NC (Notification Center of Huawei phones) takes care of the notification (when your app run in the foreground).
The NC doesn't implement the method of reading the data content as stated here by Huawei.
Therefore, you have to instruct your app, so that is your app handling the push-notification rather than the NC.
To do so you have to:
message.android.notification.foreground_show = false in the payload (See here)
You need to declare the implementation class of HmsMessageService in the AndroidManifest.xml file of the app (See here).
Here you can find all the details about the payload of a Downlink Messages

Related

Send request to an other android device in a app

I am currently trying to make a personal location application between 2 devices on Android.
The concept is simple: I install my application on my phone as well as on that of my wife and each can geolocate the other.
(This application is strictly personal)
To achieve this, I thought of using sending notifications by FCM.
Telephone A sends a request to telephone B which listens via a service for the reception of a message.
When phone B receives the request, it returns the GPS coordinates via FCM so that phone A displays them on a MAP.
(I also have the possibility to store the coordinates in a database instead of sending back an FCM message)
But FCM's documentation says:
"When your app is in the background, notification messages are displayed in the system tray, and onMessageReceived is not called. For notification messages with a data payload, the notification message is displayed in the system tray, and the data that was included with the notification message can be retrieved from the intent launched when the user taps on the notification."
Of course, this reduces the scope since it forces the user of the phone receiving the notification to click on it to activate the actions of the service.
Can FCM still meet my needs through another channel?
Are there other options to send a "request" to another phone?
(I know that this kind of application exists on the PlayStore, but I want to try to make mine :-))
The key word in that section from the documentation you quote is notification messages. Firebase Cloud Messaging supports two types of messages:
Notification messages, which are display by the system when the app is inactive - and delivered to your application code when the app is active.
Data messages, which are always delivered to your application code.
It sounds like you'll want to use data messages for this use-case

Android Notifications in the background

I am attempting to create an app that sends notifications to user while it is not the current app or open in the recent apps. I can create an application that sends notifications when the app is open, but cannot keep it going when the app is not the current app. I have attempted to use services but to no success. Any help is great.
I am testing my program on Samsung Galaxy A 50, running Android 9 Pie. I wish for my application to send notifications at a repeating time using AlarmManager. I am trying to use the service class to allow for notifications to be sent to the user while the app is closed
it seems you are receiving notifications while your app is in foreground but not in background or killed state.
Here is a quick suggestion, Instead of sending notification with "notification" keyword try to change this to "data"
Example:
{
"to": "device token"
"notification":{
"title":"Title",
"body":"Body"
},
"priority":123
}
Change to this:
{
"to": "device token"
"data":{
"title":"Title",
"body":"Body"
},
"priority":123
}
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
Source: https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive

Android - FCM problem with "notification" key

I'm with a problem here.
I have a logic with deep links in my push notification.
This is the FCM's json now:
{
"to": token_here",
"data": {
"DEEP_LINK_PATH": "STRING_HERE",
},
"notification": {
"title": "Teste",
"body": "Push de teste"
}
}
As you can see, all the logic for deep links is inside DATA, as usual.
When my app is open, everything looks fine.
But when its close, or in background, not working because its not passing inside my FirebaseMessagingService. Its seens like the notification is creating automatic by Android with base in NOTIFICATION body and title.
Is there a way to continue using this model of json and receive FirebaseMessagingService be call in background too?
Firebase Cloud Messaging has two basic types of messages:
Notifications messages
These messages are delivered to your application code when the app is active, and are handled by the system (Android in your case) when your app is not active. The system displays them as a notification in the system tray in that case.
Data messages
These messages are always delivered to your application code, no matter if the app is active or not. Your application code can either display a message when it receives a data message, or process it in any other way you see fit.
So it sounds like you're looking for a data message.
Also see:
the documentation on message types
the documentation on sending messages

Is it possible to receive FCM push notifications when app is killed?

I am developing an e-mail app in which I want that the user will get a push notification as soon as they receive new email. And for that purpose I am using FCM. I have just tried push notifications using FCM by following this link: https://www.youtube.com/watch?v=XijS62iP1Xo&t=6s in order to test what features FCM provides. But the problem I face is that the device receives push notifications when app is either in foreground or background but it won't receive any push notifications when the app is closed (swipe or clear from the task manager). I don't know how to achieve this via FCM? I want to receive push notifications just like the WhatsApp and Facebook apps.
Every kind of help is appreciated. Thanks in advance.
There are 2 types of push notifications: Data messages and Notification messages.
If you are using the Data messages you will be in charge of handling the received message and present a notification to the user (if needed of course). But in this case you might miss notifications when your app is closed.
If you are using Notification Messages, FCM is handling the message for you and directly displays a notification if the app is in background/closed.
Please see more here.
It is not possible to receive a push notification in an app that has been killed/stopped by "Force stop" in application settings:
Let me quote the response I got from Firebase Support when I posed them that question:
The Android framework advises that apps that have been stopped (i.e.
killed/force-stopped from Settings) should not be started without
explicit user interaction. FCM follows this recommendation, and thus
does not deliver messages to stopped apps. Here are some documentation
that discuss this topic:
Life of a message from FCM to the device Using Firebase Cloud
Messaging with Android O
This confirms what I observed when I tested it using a simple app.
But you should be able to get push messages from FCM when the app is in background, even if it was swiped off from Recents screen, or after system reboot. That is, unless the manufacturer made the swipe gesture to work the way "Force stop" does on your device. How you receive it in the background depends on whether the push notification contains the "notification" payload:
If it does, you will receive the "data" only if user taps on the notification and it will be delivered in the Intent extras to the activity launched by the notification action.
If it doesn't, you will receive the notification in onMessageReceived just like you do when the app is in foreground.
Some other cases when your app is not killed, but still may not receive push notifications:
there can be delays because of Doze mode if you don't use high priority for the push message
message might not be delivered if your app is background restricted
Yes only if you consider sending data payloads not notifications and handle it in onMessage()
get more info here
How to handle firebase notification on background as well as foreground?
If your App is Killed or in background,check for the Payload in your Launching Screen
in My case it is MainActivity so in onCreate() Check for Extras:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d("MainActivity: ", "Key: " + key + " Value: " + value);
}
}

unable to fetch push notifications from FCM when app is completely closed

There are 3 states that an app can be in :
1. Foreground.
2. Background(minimized)
3. Completely closed( removed from list of active applications)
This is the way I've done it :
1. Forground/Background ->
onMessageReceived(RemoteMessage remoteMessage) is where I get all the messages which I further parse.
Completely closed ->
Intent processInputIntent = getIntent();
int id = processInputIntent.get("id"); // I fetch all the parameters this way.
This method works if I don't do the following changes in the manifest.
The issue is my app is a complete webview and I need to save the state of the app and so I used
android:alwaysRetainTaskState="true"
android:launchMode="singleTask">
Is this the right way of handling things? Any help would be highly appreciated.
So going by the comments. I deduce that the reason you are not getting intent data in activity after adding android:alwaysRetainTaskState="true" is because the task is being retained by the system and system is not creating a new task so now new Root Activity. But according to FCM Docs
When your app is in the background, Android directs notification
messages to the system tray. A user tap on the notification opens the
app launcher by default.
This includes messages that contain both notification and data payload
(and all messages sent from the Notifications console). In these
cases, the notification is delivered to the device's system tray, and
the data payload is delivered in the extras of the intent of your
launcher Activity.
So, I think the solution to this issue is getting DATA PAYLOAD ONLY in onMessageReceived(). And Here is very important info regarding this method.
onMessageReceived() method will not be called if the app is in background or killed and if the message sent contains DATA and NOTIFICATION payload both.
When app is not running you will anyway receive notification. But if you want to intercept data via onMessageReceived() then you will have to create a custom app server which will send only DATA payload to fcm endpoint.
something like this:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Title" : "Title for Notification",
"body" : "Notification body can go here",
"Room" : "Something extra"
},
}
Let me know if it changes anything for you.

Categories

Resources