iOS开发phonegap之消息推送

一、安装插件:

1、phonegaplocal plugin add https://github.com/phonegap-build/PushPlugin.git


2、phonegap plugin add cordova-plugin-device


3、phonegap plugin add cordova-plugin-media

二、在js文件中调用

var pushNotification;

var app = {

    // Application Constructor

    initialize: function() {

        this.bindEvents();

    },

    // Bind Event Listeners

    //

    // Bind any events that are required on startup. Common events are:

    // 'load', 'deviceready', 'offline', and 'online'.

    bindEvents: function() {

        document.addEventListener('deviceready', this.onDeviceReady, false);

        

    },

    // deviceready Event Handler

    //

    // The scope of 'this' is the event. In order to call the 'receivedEvent'

    // function, we must explicitly call 'app.receivedEvent(...);'

   

    onDeviceReady: function() {

        app.receivedEvent('deviceready');

        app.registerN();

       

    },

    // Update DOM on a Received Event

    receivedEvent: function(id) {


        var parentElement = document.getElementById(id);

        var listeningElement = parentElement.querySelector('.listening');

        var receivedElement = parentElement.querySelector('.received');


        listeningElement.setAttribute('style', 'display:none;');

        receivedElement.setAttribute('style', 'display:block;');


        console.log('Received Event: ' + id);

    },

    registerN: function() {


        pushNotification = window.plugins.pushNotification;

        

        pushNotification.register(

                                    this.tokenHandler,

                                    this.errorHandler,

                                    {

                                      "badge":"true",

                                      "sound":"true",

                                      "alert":"true",

                                      "ecb":"onNotificationAPN"

                                    });

    },

    onNotificationAPN: function(event) {

        if ( event.alert )

        {

            navigator.notification.alert(event.alert);

        }

        

        if ( event.sound )

        {

            var snd = new Media(event.sound);

            snd.play();

        }

        

        if ( event.badge )

        {

            pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);

        }

    },

    tokenHandler: function(result) {

        alert ('success');

        alert('device token = ' + result);

    },

    errorHandler: function(error) {

        alert('register erro');

        alert('error = ' + error);

    }

};



注:如需转载注明原版地址
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值