Trust
Trust
  • 发布:2019-03-22 17:45
  • 更新:2022-09-14 13:34
  • 阅读:91582

在 uni-app 中使用 UniPush

分类:uni-app

HBuilderX 1.7.2 起,上线了 UniPush 功能。

UniPush

请认真阅读 UniPush 使用指南 及文中涉及到的相关文档说明,了解下什么是 UniPush。

在 uni-app 中使用

在 uni-app 中使用 UniPush 的话,可以通过条件编译直接使用 plus 的能力。

App.vue

较为合理的方案,是在 App.vue 中一次监听,集中处理获取推送消息后的操作。

export default {  
    onLaunch: function() {  
        // #ifdef APP-PLUS  
        const _self = this;  
        const _handlePush = function(message) {  
            // TODO  
        };  
        plus.push.addEventListener('click', _handlePush);  
        plus.push.addEventListener('receive', _handlePush);  
        // #endif  
    }  
}

回调中的处理

  • 较为常见的场景是,收到 Push 消息后根据推送消息的信息,跳转到某个指定的页面。
    uni.navigateTo({  
    url: message.payload.pagePath  
    });
  • 如果某个数据信息,需要在推送成功后同步到其它页面,就需要用到 vuex 了。

vuex

/store/index.js

export default new Vuex.Store({  
    state: {  
        pushMessage: {}  
    },  
    mutations: {  
        updatePushMessage(state, message) {  
            /**  
             * 注意:这里为了方便预览查看效果,始终对 payload 做了序列化的处理。  
             * 实际开发期中,请自行调整代码并注意发送的 payload 消息格式。  
             */   
            let payload = message.payload;  
            if (typeof payload !== 'string') {  
                message.payload = JSON.stringify(payload);  
            }  
            state.pushMessage = message || {};  
        }  
    }  
})

消息同步

在 App.vue 中更新推送消息

export default {  
    onLaunch() {  
        // #ifdef APP-PLUS  
        const _self = this;  
        const _handlePush = function(message) {  
            /**  
             * 通过 vuex 来同步页面的数据,仅做演示。  
             * 实际开发中,这里可能是跳转到某个页面等操作,请根据自身业务需求编写。  
             */  
            _self.updatePushMessage(message);  
        };  
        plus.push.addEventListener('click', function(message) {  
            plus.nativeUI.toast('push click');  
            _handlePush(message);  
        });  
        plus.push.addEventListener('receive', function(message) {  
            plus.nativeUI.toast('push receive');  
            _handlePush(message);  
        });  
        // #endif  
    },  
    methods: {  
        ...mapMutations(['updatePushMessage'])  
    }  
}  

/pages/index/index.vue 页面渲染推送消息结果

<view>  
    <text class="title">推送消息 title:{{pushMessage.title}}</text>  
    <text class="title">推送消息 content:{{pushMessage.content}}</text>  
    <text class="title">推送消息 payload:{{pushMessage.payload}}</text>  
    <text class="title">推送消息 aps:{{pushMessage.aps}}</text>  
</view>

源码

详细的示例代码,请下载附件查看。

测试发布

推送功能,涉及到第三方的 SDK 模块,因此与登录、分享等功能类似,需要打包后生效。

  1. 阅读 UniPush开通指南 开通服务
  2. manifest.json->App SDK配置,勾选“DCloud UniPush”。
  3. manifest.json->App模块权限配置,勾选 Push(消息推送)。
  4. 提交打包,自定义基座或正式打包均可。

结语

现如今各大厂商对于应用权限的管理愈发严格,在开发 Push 功能的过程中,需要仔细并且耐心。
如果使用 UniPush 的过程中遇到问题,请仔细对照文档检查后,再发帖详细描述问题。

参考文档

22 关注 分享
Geeker 小张没有名字 1***@qq.com 今天回复我了吗 正儿八经吃豆腐 Panda_2012 5***@qq.com 5***@qq.com 4***@qq.com coolfires n***@yeah.net 9***@qq.com 河南dev 霜序一七 jtshushu aliang888 一个网名 rysnone b***@163.com 6***@qq.com 1***@qq.com 全栈工程师Rone

要回复文章请先登录注册

3***@qq.com

3***@qq.com

安卓手机怎么判断是否开启了推送权限?
2021-03-29 12:04
GodKnows

GodKnows

为什么我按官方示例写的,ios接受到推送后在页面更新不了pushMessage,安卓又可以
2021-02-26 23:26
TTOOMM

TTOOMM

回复 云鹏 :
我也是 完全监听不到 vivo手机
2021-02-07 12:56
TTOOMM

TTOOMM

回复 云鹏 :
我也是 完全监听不到 vivo手机
2021-02-07 12:56
rmlx

rmlx

怎么判断用户是否在线?
2021-01-11 16:50
3***@qq.com

3***@qq.com

回复 w***@hotmail.com :
在手机上用真机调试
2020-12-19 08:35
rmlx

rmlx

怎么监控收到消息就进入某个页面呢?不用点击。
2020-12-06 11:12
5***@qq.com

5***@qq.com

good
2020-12-02 11:55
westgogo

westgogo

写在APP onLaunch 中没有效果,写在具体的某个页面,却可以。搞不懂。
App.vue
```
onLaunch: function () {
console.log('App Launch');
// #ifdef APP-PLUS
const _self = this;
const _handlePush = function (message) {
console.log(message);
if (typeof message.payload === 'string') {
message.payload = JSON.parse(payload);
}
/**
* 约定 payload 格式
* type: [naviagte switchTab , web-view] 目前只支持这三种
* path: type 为 [naviagte , switchTab,web-view] 生效
* params: String , 附加参数,如:a=1&b=2 页面跳转,则作为 url 参数传递 ,并使用 encodeURIComponent 编码,固定参数名为 payload
*
*
*
*/
message.payload = message.payload || {};
console.log(message);

};
// IOS端在客户端在运行时收到推送消息触发receive事件,离线接收到的推送消息全部进入系统消息中心。点击消息中心的消息触发click

// Android 端
// 1. 透传消息
// 若符合 {title,content,payload} 类型,则不会触发 receive 事件 , 只会显示通知栏,点击通知栏,触发 click 事件
// 若不符合格式,则触发 receive 事件,且不会显示到通知栏,并且message对象的 payload 字段将自动填充为 {title,content}
// 2. 通知消息
// 点击通知栏消息, 触发 click 事件, message 将被 包装为 {title,content,payload} ,其中 payload = {title,content}
plus.push.addEventListener('click', function (message) {
WJF.ui.alert.successTip('push click');
console.log('app push click');
_handlePush(message);
});
plus.push.addEventListener('receive', function (message) {
WJF.ui.alert.successTip('push receive');
console.log('app push receive');
_handlePush(message);
});
console.log('============消息注册完毕=========')
// #endif
},
```
pages/push/index.vue
```
listenTranMsg () {
// IOS端在客户端在运行时收到推送消息触发receive事件,离线接收到的推送消息全部进入系统消息中心。点击消息中心的消息触发click
plus.push.addEventListener('click', (msg) => {
console.log('click listener ');
console.log(msg);
this.tranMsg = JSON.stringify(msg)
});
plus.push.addEventListener('receive', (msg) => {
console.log('receive listener ');
console.log(msg);
this.tranMsg = JSON.stringify(msg)
});
uni.showToast({
title: '开始监听透传数据',
icon: 'success'
});
},
```

代码没有区别,搞不懂为啥 具体的页面中可以,onLaunch不行
2020-11-11 09:36
云鹏

云鹏

一模一样的代码,啥也收不到,心累
2020-10-23 23:01