科尔多瓦PushPlugin onNotification欧洲央行不会解雇 [英] Cordova PushPlugin onNotification ecb not fired

查看:252
本文介绍了科尔多瓦PushPlugin onNotification欧洲央行不会解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()

(请原谅我的英语不好)
我特林科尔多瓦 PushPlugin 与科尔多瓦4.2和离子测试14,它得到了成功回调每次随时间OK的字符串,但欧洲央行 onNotification 从来没有发射,并在控制台没有错误。我几乎都与没有理想......,任何一个帮助吗?

(Please excuse my poor English) I'm tring Cordova PushPlugin with Cordova 4.2 and Ionic beta 14, it got success callback every time with "OK" string, but ecb onNotification never fired, and no error at console. I almost have no ideal with that..., any one help?

推荐答案

使用在Android和iOS推送通知如下。这将正常为你工作。安装应用程序后,用户需要打开应用程序呼叫ECB方法。在iOS中,PushNotifcation的意志返回移动注册ID的结果,但是在android系统寄存器的成功方法,它将只返回OK。在Android中,onNotificationGCM方法将被调用了两个类型的事件1)RegisterId和2)通知消息。我还添加了showNotificationAPN / GCM方法与$ ionicPopup.alert显示通知弹出窗口()。

Use the following for Push Notification in Android and iOS. It will work properly for you. After install the app, user will need to open the app for call ecb methods. In iOS, PushNotifcation's register success method will returns the mobile register id in result but in android, it will return only OK. In Android, onNotificationGCM method will be called for two type event 1) RegisterId and 2) Notification Message. I have also added the showNotificationAPN/GCM method for show notification popups with $ionicPopup.alert().

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

这篇关于科尔多瓦PushPlugin onNotification欧洲央行不会解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆