Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pushNotification插件的AMD封装
/**
* Created by libo on 15/2/12.
* 依赖: framework7 和 PushPlugin
*/
/*global define, console*/
define(function (require, exports, modules) {
'use strict';
require('lib/PushNotification');
var Framework7 = require('framework7');
var pushNotification = window.plugins.pushNotification;
/**
* 向消息中心注册设备上线
*/
var register = function(){
var device = Framework7.prototype.device;
try {
pushNotification = window.plugins.pushNotification;
if (device.iphone){
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"}); // required!
}else{
pushNotification.register(successHandler, errorHandler, {"senderID":"661780372179","ecb":"onNotification"}); // required!
}
} catch(err) {
var txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
console.alert(txt);
}
};
/**
* 反注册
*/
var unRegister = function(){
pushNotification.unregister(successHandler, errorHandler);
};
/**
* iOS的APNS接收消息处理函数
* @param e
*/
var onNotificationAPN = function(e){
if (e.alert) {
console.log(e.alert);
//// showing an alert also requires the org.apache.cordova.dialogs plugin
//navigator.notification.alert(e.alert);
}
if (e.sound) {
console.log(e.sound);
//// playing a sound also requires the org.apache.cordova.media plugin
//var snd = new Media(e.sound);
//snd.play();
}
if (e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
}
};
/**
* Android的GCM接收消息处理函数
* @param e
*/
var onNotification = function(e){
console.log('EVENT -> RECEIVED:' + e.event);
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
console.log("regID = " + e.regid);
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (e.foreground) {
console.log('--INLINE NOTIFICATION--');
//// on Android soundname is outside the payload.
//// On Amazon FireOS all custom attributes are contained within payload
//var soundfile = e.soundname || e.payload.sound;
//// if the notification contains a soundname, play it.
//// playing a sound also requires the org.apache.cordova.media plugin
//var my_media = new Media("/android_asset/www/"+ soundfile);
//my_media.play();
} else {
// otherwise we were launched because the user touched a notification in the notification tray.
if (e.coldstart){
console.log('--COLDSTART NOTIFICATION--');
} else {
console.log('--BACKGROUND NOTIFICATION--');
}
}
console.log('MESSAGE -> MSG: '+ e.payload.message);
//android only
console.log('MESSAGE -> MSGCNT: '+ e.payload.msgcnt);
//amazon-fireos only
console.log('MESSAGE -> TIMESTAMP: '+ e.payload.timeStamp);
break;
case 'error':
console.log('ERROR -> MSG:' + e.msg);
break;
default:
console.log('EVENT -> Unknown, an event was received and we do not know what it is');
break;
}
};
/**
* iOS获取Token
* @param result
*/
function tokenHandler (result) {
// todo 向APPServer上报获取到的Token,并保存在Server端
console.log('token: '+ result);
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
}
/**
* 注册成功回调
* @param result
*/
function successHandler (result) {
console.log('success: '+ result);
}
/**
* 注册失败回调
* @param error
*/
function errorHandler (error) {
console.log('error: '+ error);
}
modules.exports = {
register : register,
unRegister : unRegister
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.