HarmonyOS 请求通知授权,方法返回有误?

请求通知授权,可通过requestEnableNotification的错误码判断用户是否授权。

notificationManager.isNotificationEnabled() 

但是该方法在APP未授权时返回也是true,导致后续操作失败。

阅读 471
1 个回答

请参考以下示例:

import notificationManager from '@ohos.notificationManager';
import Base from '@ohos.base';
import hilog from '@ohos.hilog';
import common from '@ohos.app.ability.common';
const DOMAIN_NUMBER: number = 0xFF00;
@Entry
@Component
struct Page {
  build() {
    Column() {
      Text('点击测试')
        .padding(30)
        .onClick(()=>{
          this.testNotification()
        })
    }
  }

  testNotification(){
    let context = getContext(this) as common.UIAbilityContext;
    notificationManager.isNotificationEnabled().then((data: boolean) => {
      console.info("isNotificationEnabled success, data: " + JSON.stringify(data));
      if(!data){
        notificationManager.requestEnableNotification(context).then(() => {
          hilog.info(DOMAIN_NUMBER, TAG, `[ANS] requestEnableNotification success`);
        }).catch((err : Base.BusinessError) => {
          if(1600004 == err.code){
            console.info(`[ANS] requestEnableNotification refused`);
          } else {
            console.error(`[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`);
          }
        });
      }
    }).catch((err : Base.BusinessError) => {
      console.error(`isNotificationEnabled fail: ${JSON.stringify(err)}`);
    });
  }
}

指导文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/notification-enable-V5

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏