1

We know that the notification message can be covered by using the HMS Core push service alone or by creating the notification message locally. The methods are as follows:

1. Create notification messages locally (referred to as local notification messages)

This can be achieved by setting the same notifyId through notificationManager.notify(notifyId, notification).

2. HMS Core push service sends notification messages (referred to as Huawei push notification messages)

For messages sent using the HMS Core push service api, notify_id is set, that is, the unique identifier of each message when the notification is displayed. When notify_id is not carried or set to -1, Push NC automatically generates a unique identifier for each message, and the message will not be overwritten; if the same notifyId is set, the function of overwriting the previous message with a new message can be realized.

Although the above two implementation methods can be used independently, there are sometimes scenarios that require local notification messages and Huawei push notification messages to overlap each other. For example, through the Huawei push notification message, the user is reminded that the price of the product they are interested in has changed. If the user has already purchased, the local notification message can set the same notify_id value as the Huawei push notification message to update the recommended message to the order of the product. Logistics information to meet the special usage scenarios of the application.

Summary of steps

1. Huawei push notification message, title: Huawei push service test message, content: price change of the product you subscribed, notify_id: 10;

2. Local notification message, title: Test local notification entry channel localNotification, content: \uD83D\uDCE6 You have a courier fast coming\uD83D\uDE80, click to view the courier information, and you can also set the delivery method >>>, notifyId :10;

3. Huawei push notification message, title: Huawei push service test message, content: the product you purchased has been signed for, notify_id: 10.

Detailed process

First of all, developers need to access the push service, please refer to the official link for details

After the client accesses the push service, the client obtains the Push Token, and then pushes messages to the application according to the Push Token returned by the Push server.

HMS Core push server sample code:

 {
    "validate_only": false,
    "message": {
        "android": {
            "notification": {
                "click_action": {
                    "type": 3
                },
                "notify_id": 10
            }
        },
        "notification": {
            "body": "您订阅的商品价格变化",
            "title": "华为推送服务测试消息"
        },
        "token": ["your push token"]
    }
}

Local application client sample code (Kotlin):

 val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

//Create a channel for sending messages, "localNotification"

 var channelId = "localNotification"
        if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
            var importance =   NotificationManager.IMPORTANCE_HIGH;
            var notificationChannel = NotificationChannel(channelId,channelId,importance)
            notificationChannel.enableVibration(true)
            notificationChannel.setShowBadge(true)
           if(manager != null){
               manager.createNotificationChannel(notificationChannel)
           }
        }

//Send message to channel channelId

 ```
            
    val mBuilder = NotificationCompat.Builder(this@MainActivity,channelId)
    mBuilder.setContentTitle("测试本地通知进入渠道$channelId") //设置通知栏标题
        .setContentText("\uD83D\uDCE6您有快递正在飞速奔来\uD83D\uDE80,点击查看快递信息,还能设置收件方式哦>>>") 
        .setWhen(System.currentTimeMillis())
        .setPriority(Notification.PRIORITY_DEFAULT) 
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_VIBRATE) 
        .setSmallIcon(R.mipmap.ic_launcher_round) 

    val notification = mBuilder.build()
    manager.notify(10, notification) //这里的notifyId与华为推送通知消息的notify_id值相同,即可实现对已经展示的相同notifyId/notify_id的通知消息进行覆盖
            
            ```

The effect is as follows:

From the above effect example, it can be seen that by keeping the notifyId and notify_id values the same, the local notification message can overwrite the previous HUAWEI push notification message, and at the same time, the HUAWEI push notification message can also overwrite the previous local notification message, so that local notification can be realized Mutual overlay function of messages and Huawei push notifications.

Precautions

When creating a notification message locally, such as notificationManager.notify(notifyId, notification), set the notifyId to be the same as the notify_id field value of the Huawei push notification message, so that the local notification message and the Huawei push notification message can overlap each other.

It should be noted that notification messages with the same notifyId/notify_id set in the same application are overwritten in the order of display, that is, the messages displayed later overwrite the messages displayed before. At the same time, the notify_id field of Huawei push can take effect only if the EMUI version is 9.1.0 and above, and the push service application version is 9.1.1 and above.

Learn more details>>

Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee

Follow us to know the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙原生应用,和开发者共同成长。