更新记录

1.0.1(2024-10-10)

  1. 增加Android版本

1.0.0(2024-10-09)

iOS原生消息推送和Android谷歌fcm消息推送


平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 3.6.8,Android:5.0,iOS:12,HarmonyOS:不确定 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

iOS原生消息推送和Android谷歌fcm消息推送

集成步骤

  1. 在Firebasehttps://console.firebase.google.com/?hl=zh-cn平台创建项目,创建项目时注意开启"为此项目启用Google Analytics(分析)"(或者在项目“项目设置”,然后在集成标签页中启用该服务)
  2. 点击创建的项目,添加Android,添加的时候Android需要绑定包名和打包证书签名的SHA-1(可选),下载Android的google-services.json,注意自定义基座或云打包时需要用绑定的包名和打包证书
  3. 拷贝demo里的nativeResources、Info.plist、AndroidManifest.xml文件到项目根目录
  4. ios勾选manifest.json里的app模块配置Push(消息推送),如果勾选了Push模块,删除nativeResources/ios/UniApp.entitlements
  5. nativeResources/ios/UniApp.entitlements文件里的aps-environment对应的值是development、production,一般开发阶段使用development,生产发布使用production,根据情况设置
  6. ios打包的.mobileprovision需要包含消息推送功能
  7. 集成插件,集成插件步骤请参考 https://www.cnblogs.com/wenrisheng/p/18323027
  8. Android的google-services.json的处理方式有2种:

8.1 联系作者将google-services.json转换为config.xml,然后替换nativeResources/android/res/values/config.xml文件

8.2 使用google-services.json的值替换config.xml的值,对应的替换值为:

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

通知说明

  • iOS的通知无论app在前台运行、后台运行或者app没启动都能收到
  • Android的fcm通知:
  1. 当app在前台运行时,通知不会显示到通知栏,会回调onMessageReceived接口,需要主动调用UTSPush.shownNotification(params)来显示到通知栏
  2. 当app在后台运行时,不会回调onMessageReceived接口,通知会自动显示到通知栏
  3. 当app没有启动时,国外版本的手机会自动显示到通知栏,国内版本手机需要开启app的“自启动”功能才会显示到通知栏

接口


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)

隐私、权限声明

1. 本插件需要申请的系统权限列表:

通知权限

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问