插件:极光推送

极光推送

使用(以下基于ionic项目进行使用)

1.创建ionic 项目

ionic start myApp tabs

2.添加平台

首先修改应用ID 使其与推送的包名对应 (config.xml 里的 widget id)

ionic platform add android

3.添加插件(注意修改APIKey)

cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git --variable API_KEY=your_jpush_appkey

4.插件的初始化(参照service.js)

window.plugins.jPushPlugin.init();

5.注意设置手机的自启管理权限

service.js

    angular.module('starter.services',[])

    .service('AppService',function(){
        //收到通知调用 
        var onReceiveNotification = function(event){
            try{
                var alertContent
                if(device.platform == "Android"){
                     alertContent = window.plugins.jPushPlugin.receiveNotification.alert;
                }else{
                    alertContent   = event.aps.alert;
                }
                console.log('document onReceiveNotification');              
                console.log(alertContent);
            }catch(exeption){
                console.log(exception)
            }
        };
         //打开通知调用   
        var onOpenNotification = function(event){
            try{
                var alertContent
                if(device.platform == "Android"){
                    alertContent=window.plugins.jPushPlugin.openNotification.alert;
                }else{
                    alertContent   = event.aps.alert;
                }
                console.log('document receiveNotification');            
                console.log(alertContent);
                
            }
            catch(exception){
                console.log("JPushPlugin:onOpenNotification"+exception);
            }
        };
        //收到信息调用
        var onReceiveMessage = function(event){
            try{
            
                var message
                if(device.platform == "Android"){
                     message = window.plugins.jPushPlugin.receiveMessage.message;
                }else{
                     message   = event.content;
                }                    
                console.log('document openNotification');               
                console.log(message);
                 
            }
            catch(exception){
                console.log("JPushPlugin:onReceiveMessage-->"+exception);
            }
        };
        
        var onTagsWithAlias = function(event){
           try{
               console.log("onTagsWithAlias");    
               var result="result code:"+event.resultCode+" ";
               result+="tags:"+event.tags+" ";
               result+="alias:"+event.alias+" ";
               console.log(result)
           }
           catch(exception){
               console.log(exception)
           }
        };
       
        this.initPush = function(){
            try{
                window.plugins.jPushPlugin.init();  
                //用于开启调试模式,可以查看集成JPush过程中的log,如果集成失败,可方便定位问题所在
                window.plugins.jPushPlugin.setDebugMode (true)
                //用于android收到应用内消息的回调函数(请注意和通知的区别),该函数不需要主动调用
                
                window.plugins.jPushPlugin.receiveMessageInAndroidCallback = function(data){
                    console.log('receiveMessageInAndroidCallback');
                    console.log(data);
                };
                
                //当点击android手机的通知栏进入应用程序时,会调用这个函数,这个函数不需要主动调用,是作为回调函数来用的
                window.plugins.jPushPlugin.openNotificationInAndroidCallback = function(data){
                    console.log('openNotificationInAndroidCallback');
                    console.log(data);
                }
                
                document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);   
                document.addEventListener("jpush.setTagsWithAlias", onTagsWithAlias, false);
                
                //若有 openNotificationInAndroidCallback 则此方法不调用
                document.addEventListener("jpush.openNotification", onOpenNotification, false);
                //若有 receiveMessageInAndroidCallback 则此方法不调用
                document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
            }catch(exception){
                console.log("JPushPlugin:init-->"+exception);
            }
        };
        //tags 为数组,alias 为字符串
        this.setTagsWithAlias = function(tags,alias){
            try{
                window.plugins.jPushPlugin.setTagsWithAlias(tags,alias);
            }catch(exception){
                console.log("JPushPlugin:setTagsWithAlias-->"+exception);
            }
        }
    });

相关网站

极光控制台

https://www.jpush.cn/push/apps/4da4d7c81227935424099c97/push/notification/sent

极光文档

http://docs.jpush.io/

插件

https://github.com/jpush/jpush-phonegap-plugin

转载于:https://www.cnblogs.com/final-elysion/p/6073011.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值