unipush 推送代码

配置文件中设置 push 图标


 /* SDK配置 */
            "sdkConfigs" : {
                "ad" : {},
                "push" : {
                    "unipush" : {
                        "icons" : {
                            "small" : {
                                "ldpi" : "unpackage/res/push/18x18.png",
                                "mdpi" : "unpackage/res/push/24x24.png",
                                "hdpi" : "unpackage/res/push/36x36.png",
                                "xhdpi" : "unpackage/res/push/48x48.png",
                                "xxhdpi" : "unpackage/res/push/72x72.png"
                            }
                        }
                    }
                }
            },
 在app.vue  methods里加入设置手机通知权限方法,在onShow里调用

 /**
     * 设置手机通知权限
     */
    setPermissionsInform() {
        // #ifdef APP-PLUS  
        if (plus.os.name == 'Android') { // 判断是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");
            //android.support.v4升级为androidx
            if (NotificationManagerCompat == null) {
                NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
            }
            var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
            // 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置  
            if (!areNotificationsEnabled) {
                uni.showModal({
                    title: '通知权限开启提醒',
                    content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
                    showCancel: false,
                    confirmText: '去设置',
                    success: function(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') { // 判断是ISO
            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);
            if (isOn == false) {
                uni.showModal({
                    title: '通知权限开启提醒',
                    content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
                    showCancel: false,
                    confirmText: '去设置',
                    success: function(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  
    } ,
    /**
在app.vue的onLaunch中
// #ifdef APP-PLUS
			const that = this;
			// 獲取cid,设备标识(之后要移到登录位置,将cid和用户绑定)
			let cid = null
			plus.push.getClientInfoAsync((info) => {
				cid = info.clientid;
			}, err => {});

				//用户点击推送消息
			plus.push.addEventListener('click', (msg) => {
				// icon角标-1
				if (that.num) {
					that.num -= 1
				} else {
					that.num = 0
				}
				plus.runtime.setBadgeNumber(that.num);
				/*
				逻辑代码补充
				*/
			   // 页面跳转
				uni.navigateTo({
					url: '/pages/changePassWord/changePassWord'
				});
			}, false);
			// 客户端接收推送消息
			plus.push.addEventListener('receive', (msg) => {
				//icon角标 + 1
				that.num += 1
				plus.runtime.setBadgeNumber(that.num)
				/*
				逻辑代码补充
				*/
				// 生成通知消息
				plus.push.createMessage(msg.content, msg.payload, {
					title: msg.title,
					cover: false,
				})
			}, false);
			// #endif  

push推送图标的要求:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值