更新记录
1.0.6(2024-12-04)
- ios增加修改消息通知图标功能
1.0.3(2024-11-30)
- ios增加获取消息通知、删除消息通知接口
1.0.2(2024-10-12)
- 修复Android云打包编译问题
查看更多
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:5.0,iOS:12,HarmonyNext:不确定 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
鸿蒙元服务 |
× |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
iOS原生消息推送和Android谷歌fcm消息推送
集成步骤
- 在Firebasehttps://console.firebase.google.com/?hl=zh-cn平台创建项目,创建项目时注意开启"为此项目启用Google Analytics(分析)"(或者在项目“项目设置”,然后在集成标签页中启用该服务)
- 点击创建的项目,添加Android,添加的时候Android需要绑定包名和打包证书签名的SHA-1(可选),下载Android的google-services.json,注意自定义基座或云打包时需要用绑定的包名和打包证书
- 拷贝demo里的nativeplugins、nativeResources、Info.plist、AndroidManifest.xml文件到项目根目录
- ios勾选manifest.json里的app模块配置Push(消息推送)
- nativeResources/ios/UniApp.entitlements文件里的aps-environment对应的值是development、production,一般开发阶段使用development,生产发布使用production,根据情况设置
- 修改nativeplugins/wrs-notifiction的文件(ios自定义推送消息图标icon功能,用不到此功能可忽略这一步):
可参考uniapp官方集成文档https://nativesupport.dcloud.net.cn/NativePlugin/course/package.html#ios-extension
- manifest.json app原生插件配置勾选WRSNotification插件
- ios-extension.json里的identifier改为打包包名+自定义后缀(${包名}.${自定义后缀}),如:
包名为com.wrs.project.WRSVoipProject
后缀为WRSNotificactionServiceExtension
identifier为com.wrs.project.WRSVoipProject.WRSNotificactionServiceExtension
- ios-extension.json里的profile替换为iOS的extension签名文件名,文件名要以ios-为前缀
- ios-com_notification.mobileprovision替换为ios-extension.json里profile的签名文件
{
"token": "xx", // 远程消息推送的手机token,由app生成token
"alert": "您有一个音视频来电",
"body": "音视频来电",
"topic": "com.wrs.project.WRSVoipProject", // app包名
"sound": "ring.wav", // 消息通知铃声
"payload": {
"updateNotification": { // 修改消息通知的图标和标题,mutableContent需要设置为1,仅支持iOS 15以上系统
"icon": "http://192.168.0.104:3000/static/wechat.png",
"displayName": "张三来电"
}
},
"mutableContent": 1
}
- Android的google-services.json放到nativeResources/android文件夹下:
- 集成插件,集成插件步骤请参考
https://www.cnblogs.com/wenrisheng/p/18323027
通知说明
- iOS的通知无论app在前台运行、后台运行或者app没启动都能收到
- Android的fcm通知:
- 当app在前台运行时,通知不会显示到通知栏,会回调onMessageReceived接口,需要主动调用UTSPush.shownNotification(params)来显示到通知栏
- 当app在后台运行时,不会回调onMessageReceived接口,通知会自动显示到通知栏
- 当app没有启动时,国外版本的手机会自动显示到通知栏,国内版本手机需要开启app的“自启动”功能才会显示到通知栏
- 系统发出通知的图标和颜色在AndroidManifest.xml里配置,参考demo
接口
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;
}
})
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))
}
})
}
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)
UTSPush.getDeliveredNotifications((resp)=>{
// {"deliveredNotifications":[{"date":1732975328.0408335,"request":{"identifier":"AF2B91D8-092A-450E-8C9E-BF25BD5E852A","content":{"body":"支付结果通知","summaryArgumentCount":0,"userInfo":{"age":12,"name":"张三","aps":{"alert":"支付结果通知","sound":"ring.wav"}},"categoryIdentifier":"","title":"","summaryArgument":"","launchImageName":"","subtitle":"","threadIdentifier":""}}},{"date":1732975319.8605058,"request":{"identifier":"8092F37E-1DCF-4BDA-A929-C57B7C56D835","content":{"userInfo":{"age":12,"name":"张三","aps":{"alert":"支付结果通知","sound":"ring.wav"}},"body":"支付结果通知","summaryArgumentCount":0,"categoryIdentifier":"","title":"","summaryArgument":"","launchImageName":"","subtitle":"","threadIdentifier":""}}}]}
const deliveredNotifications = resp.deliveredNotifications
})
const identifier = “”
UTSPush.removeDeliveredNotifications([identifier])
UTSPush.removeAllDeliveredNotifications()