您的位置:首页 > 移动开发 > IOS开发

iOS开发phonegap之消息推送

2015-06-29 16:33 309 查看
一、安装插件:

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);
}
};

注:如需转载注明原版地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: