Mobile Development 9 min read

HarmonyOS Push Notification Integration Guide: Sohu News Implementation

This guide details how Sohu News integrated HarmonyOS push notifications—covering DevEco Studio setup, certificate generation, profile configuration, client ID metadata, permission requests, token retrieval, and handling cold‑ and hot‑start push data—using the HarmonyOS NEXT SDK and AppGallery Connect services.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
HarmonyOS Push Notification Integration Guide: Sohu News Implementation

This article provides a comprehensive guide to integrating push notifications into HarmonyOS applications, based on the real-world implementation by Sohu News. Sohu News, as a HarmonyOS partner, successfully launched their app on the HarmonyOS single-framework market in December 2023 as part of the initial wave of HarmonyOS applications.

Development Environment:

IDE: DevEco Studio NEXT Developer Beta2 Build Version: 5.0.3.502 (July 28, 2024)

SDK: HarmonyOS NEXT Developer Beta2 SDK, based on OpenHarmony SDK Ohos_sdk_public 5.0.0.22 (API Version 12 Canary3)

Integration Steps:

1. Generate signing certificates: First generate the P12 certificate, then the CSR certificate - the opposite order from iOS certificate generation.

2. Apply for development and release certificates through AppGallery Connect under "Users and Access" > "Certificate Management". A maximum of two development certificates and one release certificate can be created.

3. Apply for development and release profiles via "HarmonyOS App > HAP Provision Profile Management".

4. Configure application signing certificate fingerprints: In DevEco Studio, click the fingerprint icon next to "Store file(*.p12)" to automatically generate the certificate fingerprint, then add it in Project Settings > General > Application > SHA256 Certificate/Public Key Fingerprint. The configuration takes effect after approximately 10 minutes.

5. Enable push service in AppGallery Connect under "Growth > Push Service".

6. Configure Client ID and action in src/main/module.json5 by adding metadata with client_id and action values. The client_id can be obtained from AppGallery Connect, and the action value must match the server-side push message action.

7. Request user push permission in the onWindowStageCreate method:

notificationManager.requestEnableNotification(this.context).then(() => { console.info("requestEnableNotification success"); }).catch((err: Base.BusinessError) => { console.error(`requestEnableNotification fail: ${JSON.stringify(err)}`); });

8. Get PushToken in the EntryAbility class onCreate method:

export default class EntryAbility extends UIAbility { async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); GlobalContext.getContext().setObject("context", this.context); try { const pushToken: string = await pushService.getToken(); hilog.info(0x0000, 'testTag', 'Get push token successfully: %{public}s', pushToken); //保存pushToken 并 上传至应用服务端 } catch (err) { let e: BusinessError = err as BusinessError; hilog.error(0x0000, 'testTag', 'Get push token catch error: %{public}d %{public}s', e.code, e.message); } }

9. Receiving Push Notifications:

Cold Start: Use the want.parameters method in EntryAbility's onCreate to obtain push data. The landing page link is stored in the uri field. Store it in a singleton object and perform the jump after the homepage loads.

onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); //处理push,获取push中的数据 let pushLink: string = (want.parameters as Record<string, string>)['uri'] || ''; //跳转落地页:单例存储跳转链接,在首页加载完成后,读取链接完成跳转 SNPushManager.instance().pushLink = pushLink; }

Hot Start: Unlike cold start, hot start retrieves the push landing page link in the onNewWant method. Since the application is already running, the jump can be completed directly.

onNewWant(want: Want): void { let pushLink: string = (want.parameters as Record<string, string>)['uri'] || ''; hilog.info(0x0000, 'testTag', 'Get message data successfully: %{public}s', JSON.stringify(want.parameters)); //跳转落地页 openProtocolURL(pushLink); }

10. Push Message Body Structure:

{ "pushOptions": { "testMessage": true }, "payload": { "notification": { "category": "MARKETING", "title": "搜狐新闻", "body": "推送内容", "clickAction": { "actionType": 1, "data": { "uri": "端内跳转链接" }, "action": "和客户端action保持一致" } } }, "target": { "token": [ "push token" ] } }

During development, testMessage should be set to true with a daily limit of 1,000 messages. After publishing to the app store, it should be set to false. The action must match the client-side action. Excluding user subscriptions and IM messages, news-type pushes are limited to 5 per user per day.

Through these steps, HarmonyOS push notification development can be completed. The Sohu News HarmonyOS Phase 2 has also been successfully launched on the Huawei AppGallery.

mobile developmentPush NotificationHarmonyOSArkTSDevEco StudioAppGallery ConnectHMS PushUIAbility
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.