写点什么

鸿蒙开发日记之 Push Kit 实现美颜相机消息推送

作者:yimapingchuan
  • 2025-06-15
    广东
  • 本文字数:894 字

    阅读完需:约 3 分钟

一、功能背景

在美颜相机 App 中,需要通过消息推送向用户发送:

照片处理完成通知

新滤镜上线提醒

好友互动消息

 

HarmonyOS 的 Push Kit(应用服务类能力)提供高到达率的系统级推送通道,支持华为设备离线消息托管,日均节省服务器资源约 37%(华为官方数据)。

 

二、开发实录

 

json

"abilities": [{

  "permissions": [

    "ohos.permission.RECEIVE_PUSH",

    "ohos.permission.PUSH_NOTIFICATION"

  ]

}]

 

import push from '@ohos.app.push';

import notificationManager from '@ohos.notificationManager';

 

// 步骤1:初始化Push服务

async function initPush() {

  try {

    await push.getPushToken().then((token: string) => {

      console.log(`设备Token: ${token}`); // 需上传至业务服务器

    });

  } catch (error) {

    console.error(`Push初始化失败: ${error.code}`);

  }

}

 

// 步骤2:发送美颜完成通知

async function sendBeautyNotification(photoId: string) {

  const notificationRequest: notificationManager.NotificationRequest = {

    content: {

      title: "照片处理完成",

      text: "您的美颜照片已生成,点击查看",

      additionalData: {  // 自定义参数

        photoId: photoId

      }

    },

    id: 1  // 通知ID需唯一

  };

 

  await notificationManager.publish(notificationRequest).then(() => {

    console.log("通知发送成功");

  });

}

 

// 步骤3:处理通知点击(在EntryAbility中)

onCreate(want: Want) {

  if (want.parameters?.notificationAdditionalData) {

    const photoId = want.parameters.notificationAdditionalData.photoId;

    routeToPhotoDetailPage(photoId); // 跳转到照片详情页

  }

}

 

三、进阶技巧

标签分组:实现精准推送

 

push.subscribeToTopic({ topic: "vip_user" }); // VIP 用户专属通知

 

数据统计:在 AGC 控制台查看:

消息到达率

用户点击热力图

厂商通道对比:

特性 Push Kit 第三方推送

华为设备到达率 99.6% 82.3%

离线消息保留时长 72 小时 24 小时

 

用户头像

yimapingchuan

关注

还未添加个人签名 2025-03-14 加入

还未添加个人简介

评论

发布
暂无评论
鸿蒙开发日记之Push Kit实现美颜相机消息推送_HarmonyOS NEXT_yimapingchuan_InfoQ写作社区