美文网首页
JPush推送

JPush推送

作者: 一个好昵称X | 来源:发表于2019-03-26 17:06 被阅读0次

移动端获取jPush推送消息,并点击进入详情,首先要设置别名和标签、获取设备id绑定到后台,才可以接收推送消息。
具体参考官方文档: http://docs.jiguang.cn/jpush/client/client_plugins

        //JPush推送
        document.addEventListener("deviceready", () => {
            if (ons.platform.isIOS()) { //清空IOS中应用角标,服务器和本地
                window.JPush.resetBadge();
                window.JPush.setApplicationIconBadgeNumber(0);
            }

            window.JPush.init();
            window.JPush.resumePush();//恢复推送

            //设置别名与标签
            window.JPush.setTags({ sequence: 1, tags: tags});
            window.JPush.setAlias({ sequence: 1, alias: alias});

            //设置别名标签的监听事件
            document.addEventListener("jpush.setTagsWithAlias", function (event: any) {
                console.log("result code:" + event.resultCode + "tags:" + event.tags + "alias:" + event.alias);
            }, false);

            //获取设备ID
            window.JPush.getRegistrationID(function (data: any) {
                try {
                    registrationID = data;
                    localStorage.setItem("registrationId", data); // 根据需要存到本地
                    if (data.length == 0) {
                        console.log("JPushPlugin:registrationID length is 0");
                    } else {
                        console.log("JPushPlugin:registrationID length is", registrationID);
                        //调用接口进行绑定
                    }
                } catch (exception) {
                    console.log(exception);
                }
            });

            //点击消息进入详情页
            document.addEventListener("jpush.openNotification", (event: any) => {
                console.log("获取到的消息内容:",event);

                if (device.platform == "Android") {
                    //获取消息ID,用于获取详情
                    messageId = event.extras.push_page_url;
                    openMessageType = event.extras.push_type;

                    if (messageId && openMessageType === 'message') {
                        // 获取消息详情
                    }
                    //无详情消息内容直接打开App
                    else if (openMessageType === 'onlyopenapp') {
                        window.navigator.resetPage({
                            key: 'key',
                            component: component,
                        }, null);
                    }


                } else {  // IOS
                    messageId = event.push_page_url;
                    openMessageType = event.push_type;

                    if (messageId && openMessageType === 'message') {
                        // 获取消息详情
                    }
                    //无详情消息内容直接打开App
                    else if (openMessageType === 'onlyopenapp') {
                      window.navigator.resetPage({
                            key: 'key',
                            component: component,
                        }, null);
                    }

                }

            }, false);
            //获取应用版本信息,进行版本对比检查
            (window as GlobalDefinitions).chcp.getVersionInfo(this.getVersionInfoCallback.bind(this));
        }, false);

        document.addEventListener("resume", () => {
            if (ons.platform.isIOS()) {//清空IOS中应用角标,服务器和本地
                window.JPush.resetBadge();
                window.JPush.setApplicationIconBadgeNumber(0);
            }
        }, false);

相关文章

网友评论

      本文标题:JPush推送

      本文链接:https://www.haomeiwen.com/subject/bglnvqtx.html