|
|
4 ay önce | |
|---|---|---|
| .. | ||
| utssdk | 4 ay önce | |
| changelog.md | 4 ay önce | |
| encrypt | 4 ay önce | |
| package.json | 4 ay önce | |
| readme.md | 4 ay önce | |
| google-services.json | config.xml |
|---|---|
| client_type为3的client_id | default_web_client_id |
| project_info -> project_number | gcm_defaultSenderId |
| api_key -> current_key | google_api_key |
| client -> client_info -> mobilesdk_app_id | google_app_id |
| api_key -> current_key | google_crash_reporting_api_key |
| project_info -> storage_bucket | google_storage_bucket |
| project_info -> project_id | project_id |
import {
UTSPush
} from "@/uni_modules/wrs-uts-push"
设置消息回调
UTSPush.onCallback((resp) => {
this.showMsg("onCallback:" + JSON.stringify(resp))
let opt = resp.opt
switch (opt) {
// 获取到token
case "onToken":
let token = resp.token
if (token) {
// token上传给后端接口服务器,服务器调用苹果或Google的接口发送通知
}
break;
// 获取token失败
case "onTokenFail":
break;
// onWillPresent仅支持iOS
case "onWillPresent":
break;
// app运行中点击了消息,仅支持iOS,Android通过getUTSIntentData接口获取
case "onClick":
break;
// app没有启动时,点击消息启动,仅支持iOS,Android通过getUTSIntentData接口获取
case "onLaunchOptioins":
let remoteNotification = resp.remoteNotification
if(remoteNotification) {
// 点击通知启动的app
}
break;
// 仅支持Android
case "onMessageReceived":
// {"message":{"originalPriority":1,"priority":1,"from":"1062023367861","sentTime":1728529512606,"messageId":"0:1728529526496732%8b5c45cc8b5c45cc","collapseKey":"com.wrs.project.jpush","data":{"age":"999","name":"wrs","address":"zh"},"senderId":"1062023367861","ttl":2419200,"notification":{"channelId":"channel01","title":"aaaa","body":"aaaaa"}},"opt":"onMessageReceived"} let message = resp.message
let message = resp.message
let data = message.data
let notification = message.notification
if (notification) {
// app在前台运行时,收到通知不会显示到通知栏
// 需要自己调用本地通知来显示通知
this.shownMessageNotification(message)
}
break;
default:
break;
}
})
请求通知权限,仅支持iOS
if (this.isAndroid) {
let version = UTSPush.getBuildSDKVersion()
if (version >= 33) {
this.requestPermission([
"android.permission.POST_NOTIFICATIONS"
])
}
// 没有打开通知,提示用户开启通知
let isEnable = UTSPush.getNotificationsEnable()
if (!isEnable) {
this.showModel("是否去打开通知权限?", () => {
UTSPush.openAppNotificatioinSettings()
}, () => {
})
}
} else {
UTSPush.requestAuthorization({
types: ["badge", "sound", "alert"]
}, (resp) => {
let flag = resp.flag
if (flag) { // 请求权限失败
} else {
console.log("requestAuthorization:" + JSON.stringify(resp))
}
})
}
获取token
if (this.isAndroid) {
UTSPush.getToken((resp) => {
let token = resp.token
this.showMsg("getToken:" + JSON.stringify(resp))
})
} else {
// token结果从UTSPush.onCallback里回调
UTSPush.registerForRemoteNotifications()
}
点击通知打开app,获取通知数据,仅支持Android
if (this.isAndroid) {
let intentData = UTSPush.getUTSIntentData()
if (intentData) {
// 后端或本地发送通知的时候,自己带上业务参数,这里可以获取到点击通知启动app的参数
// {"extras":{"age":"999","short_cut_class_name":"io.dcloud.PandoraEntry","google.ttl":2419200,"collapse_key":"com.wrs.project.jpush","__intetn_orientation__":2,"google.message_id":"0:1728539699533701%8b5c45cc8b5c45cc","google.delivered_priority":"high","gcm.n.analytics_data":{"empty":false,"parcelled":false,"size":0},"google.sent_time":1728539699510,"from":"1062023367861","name":"wrs","address":"zh","google.original_priority":"high"}} let extras = intentData.extras
if (intentData.extras) {
}
this.showMsg("intent data:" + JSON.stringify(intentData))
}
}
app在前台运行时,Android的消息通知不会显示在通知栏,需要自己发送本地通知才能显示在通知栏
let notification = message.notification
let title = notification.title
let body = notification.body
let data = message.data
let url = "wrsapp://wrs.app"
if (data) {
let params = ""
for (let key in data) {
if (data.hasOwnProperty(key)) {
let value = data[key]
if (params.length > 0) {
params = params + "&" + key + "=" + value
} else {
params = params + key + "=" + value
}
}
}
url = url + "?" + params
}
let channelId = notification.channelId
if (!channelId) { // android 8以后一定要有channel
channelId = this.channelId + ""
}
let params = {}
params.identifier =
"123" // 通知ID,主要用于修改、删除通知,android里的identifier必须是数字,iOS的identifier可以是任意字符串
params.channel = { // 如果已经channelId的channel则不创建,如果没有则会自动创建
channelId: channelId,
channelName: "支付channel",
importance: 4, // 3: default 4: high 2: low 5: max 1: min 0: none
lockscreenVisibility: 1, //1: public 0: private -1: secret
description: "收付款通知channel"
}
params.notification = {
channelId: channelId, // 此消息通知是属于哪个channel的
contentTitle: title,
contentText: body,
visibility: 1, // 1: public 0: private -1: secret
smallIcon: { // 小图标,必传
type: "resource", // 固定
defType: "drawable", // 固定
name: "not" // 文件名,不要带文件后缀,对应nativeResources/android/res/drawable文件夹下的图片
},
autoCancel: true, // 点击通知后是否自动消息
priority: 1, // 1: high 0: default -1: low -2:min 2: max
contentIntent: {
intent: {
pkg: "com.wrs.project.jpush", // app包名
data: url,
extra: data
}
}
}
UTSPush.shownNotification(params)