From c2bfc1c66a4183e0ef806761020f2c83e7630476 Mon Sep 17 00:00:00 2001 From: chen <2659004835@qq.com> Date: Thu, 18 Aug 2022 14:21:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 67 +++++++++++++++++++++++++++++- jsFile/push.js | 4 ++ manifest.json | 26 ++++++++---- pages/tabbar/pagehome/pagehome.vue | 10 +++++ 4 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 jsFile/push.js diff --git a/App.vue b/App.vue index 9b2f058..51f8e2a 100644 --- a/App.vue +++ b/App.vue @@ -15,6 +15,7 @@ // #ifdef MP-WEIXIN this.globalData.bgmMusic = wx.createInnerAudioContext(); // #endif + this.checkNotification(); }, onShow: function() { if(uni.getStorageSync('token')){ @@ -31,7 +32,71 @@ }, methods:{ - + /** + * 检测通知栏 + */ + checkNotification:function(){ + if(plus.os.name.toLowerCase() == 'ios'){ + this.checkIosNotification(); + } + else{ + this.checkAndroidNotification(); + } + }, + checkAndroidNotification:function(){ + var main = plus.android.runtimeMainActivity(); + var pkName = main.getPackageName(); + var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat"); + var packageNames = NotificationManagerCompat.from(main); + if (packageNames.areNotificationsEnabled()) { + // console.log('已开启通知权限'); + }else{ + uni.showModal({ + title: '提示', + content: '请先打开APP通知权限,否则无法收到消息推送', + showCancel: false, + success: function (res) { + if (res.confirm) { + var Intent = plus.android.importClass('android.content.Intent'); + var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');//可设置表中所有Action字段 + intent.putExtra('android.provider.extra.APP_PACKAGE', pkName); + main.startActivity(intent); + } + } + }); + } + }, + checkIosNotification:function(){ + var UIApplication = plus.ios.import("UIApplication"); + var app = UIApplication.sharedApplication(); + var enabledTypes = 0; + if (app.currentUserNotificationSettings) { + var settings = app.currentUserNotificationSettings(); + enabledTypes = settings.plusGetAttribute("types"); + } else { + //针对低版本ios系统 + enabledTypes = app.enabledRemoteNotificationTypes(); + } + plus.ios.deleteObject(app); + if ( 0 == enabledTypes ) { + uni.showModal({ + title: '提示', + content: '请先打开APP通知权限,否则无法收到消息推送', + showCancel: false, + success: function (res) { + if (res.confirm) { + var UIApplication = plus.ios.import("UIApplication"); + var NSURL = plus.ios.import("NSURL"); + var setting = NSURL.URLWithString("app-settings:"); + var application = UIApplication.sharedApplication(); + application.openURL(setting); + plus.ios.deleteObject(setting); + plus.ios.deleteObject(application); + } + } + }); + } + }, } }; diff --git a/jsFile/push.js b/jsFile/push.js new file mode 100644 index 0000000..eebfad7 --- /dev/null +++ b/jsFile/push.js @@ -0,0 +1,4 @@ +// 原生检测 +export default { + +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index c41652f..d50954d 100644 --- a/manifest.json +++ b/manifest.json @@ -17,34 +17,36 @@ "delay" : 0 }, /* 模块配置 */ - "modules" : {}, + "modules" : { + "Push" : {} + }, /* 应用发布信息 */ "distribute" : { /* android打包配置 */ "android" : { "permissions" : [ - "", - "", - "", - "", - "", + "", "", "", "", "", "", + "", "", + "", + "", + "", "", "", + "", "", "", "", "", "", - "", + "", "", - "", - "", + "", "" ] }, @@ -60,6 +62,12 @@ "appkey_ios" : "", "appkey_android" : "" } + }, + "push" : { + "unipush" : { + "version" : "2", + "offline" : true + } } } } diff --git a/pages/tabbar/pagehome/pagehome.vue b/pages/tabbar/pagehome/pagehome.vue index a43cf66..60a9a6d 100644 --- a/pages/tabbar/pagehome/pagehome.vue +++ b/pages/tabbar/pagehome/pagehome.vue @@ -423,6 +423,7 @@ // this.offMusic(); if(uni.getStorageSync('ifVoice')){ console.log('进入接收消息'); + this.createMessage(); // uni.showToast({ // title:'您有新消息!' // }) @@ -449,6 +450,15 @@ }); // #endif }, + // 创建本地通知栏消息 + createMessage(){ + uni.createPushMessage({ + title:'你好Y', + content:'这是测试内容', + icon:'/static/public/icon-plan-fire.png', + cover:true // 是否覆盖上一次提示的消息 + }) + }, // 播放音乐 bofang(msg){ this.bgmMusic.autoplay = true;//自动播放