HarmonyOS 鸿蒙Next 可以写一个请求推送通知权限的uts方法 类似下面这个

发布于 1周前 作者 bupafengyu 最后一次编辑是 5天前 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 可以写一个请求推送通知权限的uts方法 类似下面这个

请求Android IOS 推送通知权限

function requestPhoneNotification(callBack) { // #ifdef APP-PLUS 
  if (plus.os.name == 'Android') {
    var main = plus.android.runtimeMainActivity();
    var pkName = main.getPackageName();
    var uid = main.getApplicationInfo().plusGetAttribute("uid");
    var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
    var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
    callBack(areNotificationsEnabled) // 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置 
    if (!areNotificationsEnabled) {
      uni.showModal({
        title: '通知权限开启提醒',
        content: '您还没有开启通知权限,无法接受到消息通知,是否前往设置?',
        showCancel: true,
        cancelText: '取消',
        confirmText: '去设置',
        success: res => {
          if (res.confirm) {
            var Intent = plus.android.importClass('android.content.Intent');
            var Build =
              plus.android.importClass("android.os.Build"); //android 8.0引导 
            if (Build.VERSION.SDK_INT >= 26) {
              var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
              intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
            } else if (Build.VERSION.SDK_INT >=
              21) { //android 5.0-7.0
              var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
              intent.putExtra("app_package", pkName);
              intent.putExtra("app_uid", uid);
            } else { //(<21)其他--跳转到该应用管理的详情页
              intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
              var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
              intent.setData(uri);
            } // 跳转到该应用的系统通知设置页
            main.startActivity(intent);
          }
        }
      });
    }
  } else if (plus.os.name == 'iOS') {
    var isOn = undefined;
    var types = 0;
    var app = plus.ios.invoke('UIApplication', 'sharedApplication');
    var settings = plus.ios.invoke(app, 'currentUserNotificationSettings');
    if (settings) {
      types = settings.plusGetAttribute('types');
      plus.ios.deleteObject(settings);
    } else {
      types = plus.ios.invoke(app, 'enabledRemoteNotificationTypes');
    }
    plus.ios.deleteObject(app);
    isOn = (0 != types);
    callBack(isOn)
    if (isOn == false) {
      uni.showModal({
        title: '通知权限开启提醒',
        content: '您还没有开启通知权限,无法接受到消息通知,是否前往设置?',
        showCancel: true,
        cancelText: '取消',
        confirmText: '去设置',
        success: res => {
          if (res.confirm) {
            var app = plus.ios.invoke('UIApplication', 'sharedApplication');
            var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
            plus.ios.invoke(app, 'openURL:', setting);
            plus.ios.deleteObject(setting);
            plus.ios.deleteObject(app);
          }
        }
      });
    }
  } // #endif 
 }

更多关于HarmonyOS 鸿蒙Next 可以写一个请求推送通知权限的uts方法 类似下面这个的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复
使用uts调用鸿蒙原生的API,参考uniapp官网https://uniapp.dcloud.net.cn/tutorial/harmony/native-api.html
鸿蒙请求通知授权的开发步骤,参考官方文档示例
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V13/notification-enable-V13

更多关于HarmonyOS 鸿蒙Next 可以写一个请求推送通知权限的uts方法 类似下面这个的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next系统中,你可以使用ArkTS(Ark TypeScript)来编写请求推送通知权限的方法。以下是一个示例代码,展示了如何请求推送通知权限:

import app from '@ohos.application';
import notification from '@ohos.notification';
import permission from '@ohos.permission';

@Entry
@Component
struct MainAbility {
    @State message: string = "Checking permission...";

    build() {
        Column() {
            Text(this.message)
                .fontSize(20)
                .textAlign(TextAlign.Center)
        }
        .onAppear(() => {
            this.requestNotificationPermission();
        });
    }

    private async requestNotificationPermission(): Promise<void> {
        let hasPermission = await permission.checkSelfPermission("ohos.permission.NOTIFY");
        if (hasPermission !== permission.PermissionState.GRANTED) {
            let requestResult = await permission.requestPermissionFromUser({
                permissions: ["ohos.permission.NOTIFY"],
                reason: "We need this permission to send you notifications.",
            });
            if (requestResult.hasPermission === permission.PermissionState.GRANTED) {
                this.message = "Notification permission granted.";
            } else {
                this.message = "Notification permission denied.";
            }
        } else {
            this.message = "Notification permission already granted.";
        }
    }
}

此代码在组件加载时检查并请求推送通知权限。如果权限未被授予,将提示用户授予权限。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部