feat:修改push通知配置

This commit is contained in:
zheng020 2026-06-16 22:28:53 +08:00
parent b2c044a282
commit 347f39c570
4 changed files with 453 additions and 315 deletions

View File

@ -1,39 +1,45 @@
<script>
import { getGlobalSocket } from '@/utils/socket'
import { emitAppReturnFromBackground } from '@/utils/backgroundRefreshBus.js'
import { getGlobalSocket } from "@/utils/socket";
import { emitAppReturnFromBackground } from "@/utils/backgroundRefreshBus.js";
// storage key
const HIDE_TIME_KEY = 'app_last_hide_time'
const HIDE_TIME_KEY = "app_last_hide_time";
export default {
onLaunch: function () {
console.log('App Launch')
console.log("App Launch");
// AI Chat
this.setPermissions();
},
onShow: function () {
console.log('App Show')
this.handleBackgroundReturn()
console.log("App Show");
this.handleBackgroundReturn();
this.getAllNotice();
},
onHide: function () {
console.log('App Hide')
console.log("App Hide");
// WebSocket
this.closeWebSocket()
this.closeWebSocket();
// onShow ""
uni.setStorageSync(HIDE_TIME_KEY, Date.now())
uni.setStorageSync(HIDE_TIME_KEY, Date.now());
//
this.getAllNotice();
},
methods: {
initWebSocket() {
const token = uni.getStorageSync('access_token')
const token = uni.getStorageSync("access_token");
if (token) {
console.log('初始化全局 WebSocket 连接')
const globalSocket = getGlobalSocket()
globalSocket.init(token)
console.log("初始化全局 WebSocket 连接");
const globalSocket = getGlobalSocket();
globalSocket.init(token);
}
},
closeWebSocket() {
console.log('关闭全局 WebSocket 连接')
const globalSocket = getGlobalSocket()
globalSocket.closeAll()
console.log("关闭全局 WebSocket 连接");
const globalSocket = getGlobalSocket();
globalSocket.closeAll();
},
/**
* 处理"从后台切回前台"事件
@ -41,21 +47,153 @@ export default {
* 若是则通知所有订阅了 useBackgroundRefresh 的页面执行刷新
*/
handleBackgroundReturn() {
const lastHide = uni.getStorageSync(HIDE_TIME_KEY) || 0
if (!lastHide) return
const lastHide = uni.getStorageSync(HIDE_TIME_KEY) || 0;
if (!lastHide) return;
// onShow
uni.removeStorageSync(HIDE_TIME_KEY)
uni.removeStorageSync(HIDE_TIME_KEY);
//
if (Date.now() - lastHide < 0) return
emitAppReturnFromBackground()
if (Date.now() - lastHide < 0) return;
emitAppReturnFromBackground();
},
//
getAllNotice() {
// 1 cid , cid
uni.getPushClientId({
success: (res) => {
// cid便
uni.setStorageSync("cid", res.cid);
console.log("客户端推送标识:", res.cid);
},
});
// 2
uni.onPushMessage((res) => {
const { type, data } = res;
if (type == "click") {
console.log('"click"-从系统推送服务点击消息启动应用事件;', res);
if (!data?.payload?.url) {
console.log(data)
uni.reLaunch({
// url: "/pagesA/index/index",
});
} else {
setTimeout(() => {
uni.navigateTo({
url: data.payload.url,
});
}, 1000);
}
}
if (type == "receive") {
console.log('"receive"-应用从推送服务器接收到推送消息事件', res);
}
});
},
setPermissions() {
// #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.v4androidx
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
},
},
};
</script>
<template>
<view class="app-container">
</view>
<view class="app-container"> </view>
</template>
<style>
@ -63,8 +201,8 @@ export default {
/* 引入 TheMiladiatorRegular 字体 */
@font-face {
font-family: 'TheMiladiatorRegular';
src: url('/static/fonts/The Miladiator Regular.ttf') format('truetype');
font-family: "TheMiladiatorRegular";
src: url("/static/fonts/The Miladiator Regular.ttf") format("truetype");
font-weight: normal;
font-style: normal;
font-display: swap;
@ -72,8 +210,8 @@ export default {
/* 引入 ZaoZiGongFangJianHei-1 字体 */
@font-face {
font-family: 'ZaoZiGongFangJianHei-1';
src: url('/static/fonts/ZaoZiGongFangJianHei-1.ttf') format('truetype');
font-family: "ZaoZiGongFangJianHei-1";
src: url("/static/fonts/ZaoZiGongFangJianHei-1.ttf") format("truetype");
font-weight: normal;
font-style: normal;
font-display: swap;
@ -81,8 +219,8 @@ export default {
/* 引入 ZaoZiGongFangJianHei-1 字体 */
@font-face {
font-family: 'JDLTYuanTiJian';
src: url('/static/fonts/JDLTYuanTiJian.ttf') format('truetype');
font-family: "JDLTYuanTiJian";
src: url("/static/fonts/JDLTYuanTiJian.ttf") format("truetype");
font-weight: normal;
font-style: normal;
font-display: swap;
@ -93,13 +231,13 @@ export default {
/* 全局字体设置 */
body {
font-family:
'JDLTYuanTiJian',
"JDLTYuanTiJian",
-apple-system,
BlinkMacSystemFont,
'PingFang SC',
'Hiragino Sans GB',
'Microsoft YaHei',
'Noto Sans SC',
"PingFang SC",
"Hiragino Sans GB",
"Microsoft YaHei",
"Noto Sans SC",
sans-serif;
}

View File

@ -1,7 +1,7 @@
'use strict';
const uniPush = uniCloud.getPushManager({
appId: "__UNI__9B0F7C5" // 你的应用appId
appId: "__UNI__F199FF4" // 你的应用appId
})
exports.main = async (event) => {
console.log(event)