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);
}
};
注:如需转载注明原版地址
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);
}
};
注:如需转载注明原版地址
相关文章推荐
- <iOS>关于viewWithTag的一点说明
- iOS 画图 以及清空
- 【Ios开发】监听通讯录发生变化
- 深度解析~iOS应用程序~生命周期
- iOS画图 在控件中画图,并实现清理
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS画图 在控件中画图
- ios 直角和圆角 并存
- 手势学习
- iOS打电话、发短信、发邮件功能开发
- iOS 纯代码适配iPhone6,6p
- IOS图像7之IOS图片、动画框架
- iOS didReceiveMemoryWarining
- IOS7 界面布局,导航条透明
- iOS 动态创建控件 并可以拖动控件
- iOS开发路线简述
- ios的category
- IOS 数据存储之 Core Data详解
- ios的@property属性
- ios的@class属性