请教下HarmonyOS 代码如何申请打开app的通知权限?

代码如何申请打开app的通知权限 现在app通知权限默认都是关的,要手动打开才行

阅读 637
2 个回答

口味你好,你可以通过配置 Notification.publish 发布通知接口的参数 NotificationRequest 中 wantAgent 属性实现。

通过配置Notification.publish发布通知接口的参数NotificationRequest中wantAgent属性实现

import notificationManager from '@ohos.notificationManager'; 
import WantAgent from '@ohos.app.ability.wantAgent'; 
 
async function publishNotification() { 
  let wantAgentInfo = { 
    wants: [ 
      { 
        bundleName: "com.example.webuseragent", // 自己应用的bundleName 
        abilityName: "EntryAbility", 
      } 
    ], 
    operationType: WantAgent.OperationType.START_ABILITIES, 
    requestCode: 1, 
  } 
  const wantAgent = await WantAgent.getWantAgent(wantAgentInfo) 
  let contentType = notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT; 
  await notificationManager.publish({ 
    content: { 
      contentType: contentType, 
      normal: { 
        title: "测试标题", 
        text: "测试内容", 
      } 
    }, 
    id: 1, 
    wantAgent: wantAgent 
  }) 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进