How Do I Get 'onmessagereceived' To Trigger On Notification (hms Push Kit) In React Native?
Solution 1:
Undate:
According to @Senthil Ssk 's answer, I've split the answer into two parts:
- If the app is running in the foreground, notification messages are transparently transmitted to the app. When your app server sends a notification message, the message is processed for display by the app. This function can be implemented by setting the message.android.notification.foreground_show field.
- If the app is offline and it is opened by a push notification click, the remote message can be retrieved by calling HmsPush.getInitialNotification. Please kindly refer to docs.
This is 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.
In addition, HMS Core Push Kit provides the feature of sending notification messages to the onMessageReceived method when your app is in the foreground. The solution is that you can set the foreground_show field in message > android > notification when using HMS Core Push Kit server API to send messages. If this field is set to true or left empty, notification messages are displayed in system tray even when your app is running in the foreground. If this field is set to false, messages can be delivered to the onMessageReceived method.
Here is an example of payload:
{"message":{"notification":{"title":"message title","body":"message body"},"android":{"notification":{"foreground_show":false,"click_action":{"type":1,"action":"com.huawei.codelabpush.intent.action.test"}}},"token":["pushtoken1"]}}
For more details, you can refer to Notification Message Display on UI.
Solution 2:
For Notification Message use the below code snippet.
If the app is opened by a push notification click, the remote message can be retrieved by calling HmsPush.getInitialNotification If the app is not opened by a push notification click, it returns null.
asyncfunctiongetInitialNotification(){
try{
console.log(awaitHmsPush.getInitialNotification())
}catch(ex){
console.log(ex)
}
}
Here's a link!
This is included in latest plugin release [Cordova Push Plugin 5.0.2 (2020-09-30)]. This will work for sure.
Solution 3:
onMessageReceived()
is called only for data messages.
Check out this part of Push Kit FAQ:
[Q1] What are differences between data messages and notification messages?
Data messages are not displayed after being sent by HUAWEI Push Kit to phones. Instead, the messages are transferred to the developer's app, and the app is responsible for parsing and displaying messages.
After a device receives a notification message, the system directly displays it in NC. The user 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.
Post a Comment for "How Do I Get 'onmessagereceived' To Trigger On Notification (hms Push Kit) In React Native?"