myApp.services.factory('GCMHelper', ($q)->
pushNotification = {}
_init = ()->
defer = $q.defer()
ionic.Platform.ready(()->
pushNotification = window.plugins.pushNotification;
window.onNotification = (res)->
console.log('onNotification', res)
defer.resolve()
)
return defer.promise
return {
register: ()->
_init().then(()->
pushNotification.register(
(res)->
console.log('gcm register success', res)
(err)->
console.log('gcm register err', err)
{
"senderID": "*********",
"ecb": "onNotification"
}
);
)
}
)
控制器中的:
GCMHelper.register()
(请原谅我的英语不好)
我将Cordova PushPlugin与Cordova 4.2和Ionic beta 14联系起来,每次使用“OK”字符串获得成功回调,但ecb onNotification
从未被解雇,并且在控制台上没有错误。我几乎没有理想......,任何一个帮助?
答案 0 :(得分:6)
在Android和iOS中使用以下推送通知。它会适合你。安装应用程序后,用户需要打开应用程序以调用ecb方法。在iOS中,PushNotifcation的注册成功方法将返回结果中的移动注册ID,但在android中,它只会返回OK。在Android中,onNotificationGCM方法将被调用两种类型事件1)RegisterId和2)Notification Message。我还使用$ ionicPopup.alert()为show notification弹出窗口添加了showNotificationAPN / GCM方法。
.run(function ($ionicPlatform, PushProcessingService) {
$ionicPlatform.ready(function () {
try {
PushProcessingService.initialize();
} catch (e) {
//hide event
}
})
})
.factory('PushProcessingService', ["$window", "$ionicPopup", function ($window, $ionicPopup) {
function onDeviceReady() {
var pushNotification = window.plugins.pushNotification;
if (ionic.Platform.isAndroid()) {
pushNotification.register(gcmSuccessHandler, gcmErrorHandler, {'senderID': 'XXXXXXXXXXXXXX', 'ecb': 'onNotificationGCM'});
} else if (ionic.Platform.isIOS()) {
var config = {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb": "pushCallbacks.onNotificationAPN"
};
pushNotification.register(gcmSuccessHandler, gcmErrorHandler, config);
}
var addCallback = function addCallback(key, callback){
if(window.pushCallbacks == undefined){
window.pushCallbacks = {};
}
window.pushCallbacks[key] = callback({registered:true});
}
}
function gcmSuccessHandler(result) {
console.log("Register push notification successfully : " + result);
if (ionic.Platform.isIOS()) {
var mobileType = "ios";
var mobileRegisterId = result;
// Save the ios mobile register Id in your server database
// call the following method on callback of save
addCallback("onNotificationAPN", onNotificationAPN);
}
}
function gcmErrorHandler(error) {
console.log("Error while register push notification : " + error);
}
return {
initialize: function () {
document.addEventListener('deviceready', onDeviceReady, false);
},
registerID: function (id) {
var mobileType = "android";
// Save the android mobile register Id in your server database
console.log("RegisterId saved successfully.");
},
showNotificationGCM: function (event) {
$ionicPopup.alert({
title: "Pajhwok Notification",
subTitle: event.payload.type,
template: event.payload.message
});
},
showNotificationAPN: function (event) {
$ionicPopup.alert({
title: event.messageFrom + "..",
subTitle: event.alert,
template: event.body
});
}
}
}])
onNotificationAPN = function(event) {
if (!event.registered) {
var elem = angular.element(document.querySelector('[ng-app]'));
var injector = elem.injector();
var myService = injector.get('PushProcessingService');
myService.showNotificationAPN(event);
} else {
console.log("Registered successfully notification..");
}
}
function onNotificationGCM(e) {
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.
var elem = angular.element(document.querySelector('[ng-app]'));
var injector = elem.injector();
var myService = injector.get('PushProcessingService');
myService.registerID(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.
var elem = angular.element(document.querySelector('[ng-app]'));
var injector = elem.injector();
var myService = injector.get('PushProcessingService');
myService.showNotificationGCM(e);
break;
case 'error':
alert('<li>ERROR :' + e.msg + '</li>');
break;
default:
alert('<li>Unknown, an event was received and we do not know what it is.</li>');
break;
}
}