HMS Push Kit handle data message when app closed (React Native) - huawei-mobile-services

Attempt to receive data message from HMS Push Kit when my app is closed.
I am able to receive data message when app is foreground:
// Registering
componentDidMount(){
this.listener = HmsPushEvent.onRemoteMessageReceived(event => {
const RNRemoteMessageObj = new RNRemoteMessage(event.msg);
const msg = RNRemoteMessageObj.parseMsgAllAttribute(event.msg);
console.log("Data message received : "+msg);
}
}
// Unregistering
componentWillUnmount(){
this.listener.remove();
}
By using this method I wont able to receive the data message when app is closed since the listener had removed.
Any ideas?

Push Kit supports two types of messages: notification messages and data messages. For the sake of saving power and not disturbing users, your app will not be launched by Push Kit after being stopped, and no data messages can be delivered to your app. In this case, you can determine whether to use notification messages based on your services. The delivery of data messages depends on the resident status of your app. However, notification messages can still be delivered even if your app is not launched.
To allow users to open a specified page of your app after they tap a notification message, please kindly refer to:
https://stackoverflow.com/a/64100678/14006527
Alternatively, you can set high-priority data messages to forcibly launch your stopped app to receive and process the messages. To do so, you need to apply for special permission by referring to the related description in FAQs.

Related

Duplicate notifications with different sounds - push notification remote

I am trying to add the custom sound in the push notification. I have added the showNotification in the background handler on the client-side (flutter), but now when the application is in the background or terminated I receive duplicate notifications, one with default sound and the other with my custom sound. Can anyone tell me how to stop showing the default background notification?
You're getting the duplicate notification because
you're sending a notification message and it is automatically displayed by the FCM SDK and,
you're implementing a custom notification display yourself.
The solution is to send a data message instead of a notification message.
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as "display messages."
These are handled by the FCM SDK automatically.
Data messages, which
are handled by the client app.
Source

HMS Push plugin - how to receive data message when the app is in killed state

Android - Huawei with HMS push plugin, HmsPushEvent.onRemoteMessageReceived this event was not triggered when app is in killed state. This event only getting called while app is in foreground and background state. Can you please tell which event will be called when app is in killed state.
Push Kit supports two types of messages: notification messages and data messages. After a device receives a data message, the device transfers it to your app instead of directly displaying the message. Your app then parses the message and triggers the corresponding action. Push Kit only functions as a channel, and the delivery of data messages depends on the resident status of your app. However, notification messages can still be delivered even if your app is not launched.
For the sake of saving power and not disturbing users, your app will not be launched by Push Kit after being stopped, and no data messages can be delivered to your app. In this case, you can determine whether to use notification messages based on your services.
To allow users to open a specified page of your app after they tap a notification message, proceed as follows:
Generate Intent parameters
Set intent in the message body on your app server
Register the Activity class to be started in the AndroidManifest.xml file of the app
Receive data in the customized Activity class
From: https://stackoverflow.com/a/64100678/14006527
Alternatively, you can set high-priority data messages to forcibly launch your stopped app to receive and process the messages. To do so, you need to apply for special permission by referring to the related description in FAQs.

send flutter firebase cloud messaging when app is completely shut down

I am following the FCM from https://pub.dev/packages/firebase_messaging
and if I am in app or I press home button and do not close app completely , the messages will be sent and every thing is good , but if I close the app completely , the messages will not be send .
what do I should doing ?
There are 2 types of FCM messages:
Notification messages
Data messages
Nofitification messages can be handled when app is terminated, but you have to add backgroundMessageHandler and also register a plugin. Follow guide on plugin docs to do this properly.
Data messages, when app is terminated and on Android are unfortunately not handled by the plugin and the message is lost. On iOS messages are stored and delivered when app comes back to foreground. The source.

How can I tell if a push notification was successfully delivered in Azure NotificationHubs?

I have an app with a simple messenger component to it. When user1 sends a message to user2 I am using Azure Notification Hubs to send a push notification to user2. Since I am using Notification Hubs to register the user's devices for push notifications, I don't know which phone OS's they have registered with, so just queue a notification for each type I support:
NotificationOutcome outcome1 = await hub.SendAppleNativeNotificationAsync(jsoniOSPayload, tags);
NotificationOutcome outcome2 = await hub.SendGcmNativeNotificationAsync(jsonAndroidPayload, tags);
...(etc.)
However, we need to handle the case where an app has been deleted. When this happens, we need to send the user an email if a push notification could not be sent to any of their devices.
My question is: how can I tell if at least one notification was successfully delivered to a users device? I know about the NotificationHubClient.EnableTestSend property, which does cause the NotificationOutcome object to have a success count. This would work perfectly, but the documentation indicates this would not be optimal in production:
"When test send is enabled, the following occurs: All notifications
only reach up to 10 devices for each send call.The Send* methods
return a list of the outcomes for all those notification deliveries.
The possible outcomes are the same as displayed in telemetry. Outcomes
includes things like authentication errors, throttling errors,
successful deliveries, and so on.This mode is for test purposes only,
not for production, and is throttled."
Any suggestions would be appreciated!
how can I tell if at least one notification was successfully delivered to a users device?
As you mentioned that NotificationHubClient.EnableTestSend is used for debugging and limited to 10 devices.
If we want to get the count of successfully delivered, we can use function NotificationHubClient.GetNotificationOutcomeDetailsAsync(string notificationId), more details please refer to document.
Demo code:
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("Endpoint=sb://notificationnamespace.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=xxxxxxx", "NotificationHub Name");
string message = "{\"title\":\"((Notification title))\",\"description\":\"Hello from Azure\"}";
var result = await hub.SendGcmNativeNotificationAsync(message); //GCM for example.
var notificationDetails = await hub.GetNotificationOutcomeDetailsAsync(result.NotificationId);
return notificationDetails;
Note: It is just for standard pricing tier.

How does WhatsApp know if the user gets the push notification even if the app is runnng in the background and is not open?

I have already asked related question:
any option to know if apple app get the push notification?
And I got an answer that it is not possible to know if push notification comes to the iPhone when application is in the background and the user does not have it opened...
But I found something interesting:
I take 2 iPhones with WhatsApp. I open, in the first iPhone, WhatsApp and exit it (the WhatsApp is in the background now), then with the second iPhone, I send WhatsApp massage to the first iPhone.
When the push notification of WhatsApp reaches first iPhone I can see two "V" (symbols) near to the message (and I don't open the Push notification). After this I close the Internet & 3G in the first iPhone and again send WhatsApp message with the second iPhone to the first one. - and I can see only one "V" near to the massage (not read - not get push notification).
Now after re-enabling the Internet on the first iPhone and after I get the push notification I can see two "V" (again - I don't open the push notification).
How does it work? How does WhatsApp know whether the iPhone gets the push notification or not while the WhatsApp works in the background?
Let us call the iPhone with WhatsApp application closed - the receiver and the other iPhone you send messages with - the sender, and let us use generic term application instead of WhatsApp
The application at the sender is not able to learn that push notification has reached the receiver. It is by the nature of push notifications.
However, the sender is possible to send another - not a push notification, but prioprietary protocol - message to the application at the receiver which it (the receiver) is welcome to acknowledge to the sender in any application-specific way it wants.
So, I would guess (I don't know - I'm not affiliated with WhatsApp in anyway) that the double-v icon you get is when application message reaches the application at the receiver - and has nothing to do with push notification.
What you mean is just the basics of push-technology :
The whatsapp software runs in the background listening on a specific port
for incoming messages. It does this continuously.
BTW :
One V = message delivered from your mobile to the whatsapp server
Two V's = message pushed from whatsapp server to recipient mobile
Blue V's indicate message has been read
Whatsapp doesn't show if the person reads the message or not. It just informs when the message has been sent to the whatsapp servers(one V) and when it has been sent the person phone (2 V's). The double does not mean the message was read it just means that whatsapp connected to the phone and was able to pass on the message.
After receiving a push notification the receiver can download data from a URL that you include in the payload of your push notification. By requesting that URL the server knows that the receiver received the push notification and can pass this status on to your sender application.
Like so:
Add a specific URL in your push notification payload
Implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method in your app delegate and in that method call the URL from the payload.
Implement a logic on your server that recognizes the URL call as a received notification
Let your sender application continuously get the current notification status from the server.
The above posters are all correct. Just to add one more thing. If Whatsapp has recently been closed, it is still running in the background and can still receive the message (hence the return receipt). However, after the app is left unused for a while (or other apps have higher priority) Whatsapp is terminated, then it will no longer receive incoming messages and will not return receipt.
Of course the observant person would notice a small lag between opening the app and the new messages being displayed which seems to suggest the messages are not downloaded in the background but only downloaded upon opening the app. So why does it return receipt while running in the background?