鸿蒙特性《鸿蒙通知服务:Godot游戏事件推送(如成就解锁)》

在游戏开发中,实时推送事件通知(如“成就解锁”“任务完成”)是提升玩家留存的重要手段。鸿蒙(HarmonyOS)的通知服务(Notification Service)支持跨设备、跨场景的消息推送,即使在游戏后台或设备重启后仍能触达用户。本文将以​​Godot引擎+鸿蒙适配​​为背景,带你实现“成就解锁通知”的完整流程,从环境配置到代码落地,新手也能轻松掌握。


一、鸿蒙通知服务:核心能力与游戏场景

鸿蒙的通知服务(@ohos.notification)提供了一套标准化的消息推送框架,核心能力包括:

  • ​多场景触达​​:支持在桌面、锁屏、通知中心显示;
  • ​自定义内容​​:可设置标题、文本、图标、进度条、按钮等;
  • ​交互能力​​:点击通知可跳转至游戏指定页面(如成就详情);
  • ​持久化存储​​:重要通知可长期保留(如赛季成就)。

在游戏中,典型应用场景包括:

  • 成就解锁(如“首次击败BOSS”);
  • 限时任务提醒(如“每日登录奖励即将过期”);
  • 系统公告(如“版本更新提示”);
  • 赛事直播通知(如“公会战即将开始”)。

二、实战准备:环境与权限配置

1. 开发环境搭建

  • ​工具​​:DevEco Studio(鸿蒙官方IDE,需4.0+版本);
  • ​SDK​​:安装鸿蒙API Level 9+的SDK组件(路径:File > Settings > SDK Manager);
  • ​Godot引擎​​:推荐4.2+版本(需支持鸿蒙导出,参考前文《鸿蒙适配:Godot项目初始化》)。

2. 权限声明(关键!)

鸿蒙通知服务需要申请ohos.permission.NOTIFICATION_CONTROLLER权限,否则无法发送通知。需在以下两个文件中声明:

(1)module.json5(鸿蒙模块配置)
{
  "module": {
    // ...其他配置
    "requestPermissions": [
      {
        "name": "ohos.permission.NOTIFICATION_CONTROLLER"
      }
    ]
  }
}
(2)config.json(应用配置)
{
  "app": {
    // ...其他配置
    "deviceTypes": [
      "phone", "tablet"  // 目标设备类型
    ],
    "deliveryWithInstall": true  // 安装时自动授予基础权限
  }
}

三、核心实现:Godot触发鸿蒙通知的完整流程

1. 设计通知触发逻辑(GDScript)

在Godot中,我们通过调用鸿蒙的原生API触发通知。由于Godot与鸿蒙的通信需通过桥接,这里采用​​鸿蒙导出模板+GDScript事件绑定​​的方式。

步骤1:创建通知管理器脚本

在Godot项目中,新建一个NotificationManager.gd脚本(挂载到根节点):

extends Node

# 鸿蒙通知服务接口(通过导出模板暴露)
var notification_service = null

func _ready():
    # 初始化鸿蒙通知服务(仅在鸿蒙平台生效)
    if Engine.is_editor_hint():
        return  # 编辑器模式跳过
    
    # 通过鸿蒙导出模板获取原生通知服务实例(伪代码,实际需根据导出模板调整)
    var native_bridge = get_node("/root/NativeBridge")  # 假设导出模板挂载了NativeBridge节点
    notification_service = native_bridge.get_notification_service()

# 发送成就解锁通知(示例)
func send_achievement_unlocked(achievement_name: String, description: String):
    if not notification_service:
        print("鸿蒙通知服务未初始化")
        return
    
    # 构建通知内容(使用鸿蒙NotificationBuilder)
    var builder = notification_service.create_builder()
    builder.set_small_icon("common_icon")  # 小图标(鸿蒙资源ID)
    builder.set_large_icon("achievement_unlocked")  # 大图标(鸿蒙资源ID)
    builder.set_title("成就解锁:" + achievement_name)
    builder.set_content(description)
    builder.set_priority(Notification.PRIORITY_HIGH)  # 高优先级(弹窗显示)
    
    # 设置点击行为:打开游戏成就详情页
    var intent = Intent(OperationOperation.ACTION_VIEW)
    intent.set_uri("harmonyos://app/mygame/achievement_detail?name=" + achievement_name)
    builder.set_click_intent(intent)
    
    # 发送通知(唯一ID,用于更新或取消)
    var notification_id = 1001  # 自定义唯一ID
    notification_service.notify(notification_id, builder.build())

2. 鸿蒙原生侧代码(ArkTS)

由于Godot无法直接调用鸿蒙API,需通过​​导出模板​​将鸿蒙原生代码与Godot绑定。以下是关键的原生代码逻辑(需在鸿蒙工程中实现):

(1)创建通知服务工具类(NotificationUtil.ets
// 导入鸿蒙通知模块
import notification from '@ohos.notification';
import common from '@ohos.app.ability.common';

export class NotificationUtil {
  private static instance: NotificationUtil = null;
  private context: common.UIAbilityContext = null;

  // 单例模式
  public static getInstance(context: common.UIAbilityContext): NotificationUtil {
    if (this.instance === null) {
      this.instance = new NotificationUtil();
      this.instance.context = context;
    }
    return this.instance;
  }

  // 创建通知构建器
  public createBuilder(): notification.NotificationBuilder {
    let builder = new notification.NotificationBuilder(this.context);
    // 设置默认样式(可选)
    builder.setSmallIcon($r('app.media.common_icon'));  // 小图标资源
    builder.setColor('#FFFFFF');  // 主题色
    return builder;
  }

  // 发送通知
  public notify(notificationId: number, notification: notification.Notification): void {
    let notificationManager = notification.getNotificationManager();
    notificationManager.notify(notificationId, notification);
  }
}
(2)导出模板集成(NativeBridge.gd

在Godot导出鸿蒙项目时,需通过自定义导出模板将上述原生代码注入。在NativeBridge.gd中添加以下方法,供GDScript调用:

# NativeBridge.gd(挂载到鸿蒙导出项目的根节点)
extends Node

# 获取鸿蒙通知服务实例(GDScript调用入口)
func get_notification_service() -> Object:
    # 调用鸿蒙原生方法(通过JNI或HSP桥接,具体实现依赖导出模板)
    # 这里简化为返回预定义的工具类实例
    return NotificationUtil.getInstance(getAbilityContext())

四、测试与调试:确保通知正常显示

1. 触发通知测试

在游戏中,当玩家解锁成就时调用send_achievement_unlocked方法:

# 在成就管理节点中调用(示例)
func _on_achievement_unlocked(achievement_name: String, description: String):
    var notification_mgr = get_node("/root/NotificationManager")
    notification_mgr.send_achievement_unlocked(achievement_name, description)

2. 常见问题排查

问题1:通知未显示
  • ​原因​​:权限未申请、图标资源未正确配置、通知ID重复。
  • ​解决​​:
    • 检查module.json5是否声明了NOTIFICATION_CONTROLLER权限;
    • 确认小图标和大图标资源已添加到鸿蒙工程的resources/base/media目录;
    • 使用唯一的notification_id(如成就ID)。
问题2:点击通知无反应
  • ​原因​​:Intent的URI格式错误、目标页面未注册。
  • ​解决​​:
    • 检查URI是否为鸿蒙应用的合法路径(如harmonyos://app/mygame/achievement_detail);
    • 在鸿蒙应用的main_pages.json中注册成就详情页路由。
问题3:鸿蒙模拟器不显示通知
  • ​原因​​:模拟器未开启通知权限、系统版本过低(需API Level 9+)。
  • ​解决​​:
    • 进入模拟器设置 > 应用 > 我的游戏 > 通知,开启“允许通知”;
    • 使用鸿蒙API Level 9+的模拟器(推荐API Level 10+)。

五、进阶功能:自定义通知样式与交互

1. 添加进度条(如限时任务)

# 在send_achievement_unlocked函数中扩展
builder.setProgress({
  type: notification.ProgressType.PROGRESS,
  current: 80,  # 当前进度
  total: 100,   # 总进度
  indeterminate: false  # 是否显示不确定进度
})

2. 添加操作按钮(如“立即查看”)

# 添加按钮(鸿蒙支持最多3个按钮)
var action1 = notification.ActionBuilder()
  .setLabel("立即查看")
  .setIcon($r('app.media.ic_view'))
  .setIntent(intent)  # 绑定点击事件
  .build()

builder.addAction(action1)

3. 定时通知(如每日登录提醒)

# 设置通知触发时间(延迟5分钟后发送)
var trigger = notification.TriggerBuilder()
  .setTimeInterval(5 * 60 * 1000)  # 5分钟(毫秒)
  .build()

notification_service.schedule(builder.build(), trigger)

总结:通知服务是游戏的“隐形助手”

本文通过“成就解锁通知”的场景,带你掌握了鸿蒙通知服务的核心用法。关键步骤包括:

  1. 申请通知权限(鸿蒙模块配置);
  2. 用GDScript触发通知逻辑;
  3. 通过鸿蒙原生代码构建通知内容;
  4. 处理通知点击交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值