0

我正在为Android/IOS做pushnotification。科尔多瓦PushNotification onNotificationGCM不叫

我已经使用了一个科尔多瓦推送插件https://github.com/phonegap-build/PushPlugin,它似乎很好。

信息:我在一个AngularJS项目。

在我NotificationHelper厂我有这个init方法:

helper.init = function() { 

    // Instanciate push plugin notification 
    var pushNotification = window.plugins.pushNotification; 

    var errorHandler = function(error) { 
     logger.debug('errorHandler = ' + error); 
    }; 

    if ($rootScope.isAndroid()) { 
     var senderId = CONFIG.NOTIFICATION_ANDROID_SENDER_ID; 
     pushNotification.register(function(result) { 
      logger.debug('successHandler = ' + result); 
     }, errorHandler, { 
      'senderID' : senderId, 
      'ecb' : 'onNotificationGCM' 
     }); 
    } 
}; 

我还定义上mains.js这些方法:

var onNotificationGCM = function(event) { 
    // call back to web service in Angular. 
    var elem = angular.element(document.querySelector('[ng-app]')); 
    var injector = elem.injector(); 
    var service = injector.get('NotificationHelper'); 
    service.onNotificationGCM(event); 
}; 

这是一招,从主要的Javascript调用angularJS工厂。

'onNotificationGCM' 叫 'NotificationHelper.onNotificationGCM' 的方法:

helper.onNotificationGCM = function(e) { 
    switch (e.event) { 
    case 'message': 
     // Notification happened while app was in the foreground 
     if (e.foreground) { 
      logger.debug('[notification] [message] Foreground : ' + JSON.stringify(e)); 
     } else { // Notification touched in the notification tray 
      logger.debug('[notification] [message] Background : ' + JSON.stringify(e)); 
      if (e.coldstart) { 
       // App was not running and user clicked on notification 
      } else { 
       // App was running and user clicked on notification 
      } 
     } 
     decriptPayloadNotification(e.payload); 
     break; 
    case 'registered': 
     logger.debug('[notification] [registered] : ' + JSON.stringify(e)); 
     if (e.regid.length > 0) { 
      registerUser(e.regid, 'gcm'); 
     } 
     break; 

    case 'error': 
     logger.debug('[notification] [error] : ' + JSON.stringify(e)); 
     break; 

    default: 
     logger.debug('[notification] [default] : Unknown, an event was received and we do not know what it is : ' + JSON.stringify(e)); 
     break; 
    } 
}; 

在第一次使用时,一切工作良好:收到

  • 通知

    • '注册' 的事件接收
    • 当我在前台我收到'消息'事件:

    NotificationHelper:[notification] [message] Foreground:{“event”:“message”,“from”:“847593779913”,“message”:“Agenêts23/03 10h \ r \ n”,“collapse_key “:”do_not_collapse“,”foreground“:true,”有效载荷“:{”lt“:”school“,”lv“:”E1154“,”notId“:”35429“,”title“:”Agenêtsle 23/03/2015" , “消息”: “Agenêts23/03 10H \ r \ n” 个}}

    • 当我在背景,如果收到通知符和TOUCHE它通知盘,收到'message'event:

    通知帮助:[通知] [信息]背景:{“event”:“message”,“from”:“847593779913”,“message”:“la piscine serafermée1journéepour raison technique”,“coldstart”:false ,“collapse_key”:“do_not_collapse”,“foreground”:false,“payload”:{“lt”:“游泳池”,“lv”:“E114”,“notId”:“29869”,“title” 23/04/2015 fermeture德拉​​鱼”,‘消息’:‘拉鱼的血清fermée1个JOURNEE倒存在的技术’}}

    ,如果我杀了我的应用程序,一切都停止工作。

    如果我重新启动应用程序,我将不会收到'message'event和'onNotificationGCM'将不会被调用。

    我创办了一些文章谈到这个问题,但没有成功:

    Stackoverflow : Phonegap PushNotification to open a specific app page

    有没有人有这个问题的想法?

  • 回答

    0

    我问自己,为什么它的工作正确的第一时间,而不是下面的时间...和我有这样的想法:

    • 也许回调函数首次订阅插件,但不是下一次。

    而这正是我的问题的来源。我只调用一次“pushNotification.register”来初始化插件并获得ID ... 但是,下次启动应用程序时,我不再插件,因此pushplugin不知道在回调请求中调用哪个方法。

    所以回答是:在每个应用程序启动中调用“pushNotification.register”!

    在我的代码,我不得不拨打以下方法对每个“deviceready”事件:

    helper.init = function() { 
    
    // Instanciate push plugin notification 
    var pushNotification = window.plugins.pushNotification; 
    
    var errorHandler = function(error) { 
        logger.debug('errorHandler = ' + error); 
    }; 
    
    if ($rootScope.isAndroid()) { 
        var senderId = CONFIG.NOTIFICATION_ANDROID_SENDER_ID; 
        pushNotification.register(function(result) { 
         logger.debug('successHandler = ' + result); 
        }, errorHandler, { 
         'senderID' : senderId, 
         'ecb' : 'onNotificationGCM' 
        }); 
    } 
    }; 
    
    0

    如果使用节点-GCM的推送通知服务器端我希望这可以帮助你的问题: 检查下列事项:

    • 使用亚行logcat从控制台和检查设备的日志,看看如果您的设备收到任何通知(如果您收到,您应该看到gcmservices的一些日志)。我相信你应该看到一些日志,否则当应用程序在前台时你可能不会收到任何通知。
    • 检查,如果你添加“称号”,并在你的消息数据“信息”键(此画我发疯之前)
    • 检查,如果delayWhileIdle参数是假

    样本信息的变量和方法我现在用的就是如下:

    var pushAndroidSender = new gcm.Sender('Your GoogleApi Server Key Here'); 
    var message = new gcm.Message({ 
        collapseKey: 'demo', 
        delayWhileIdle: false, 
        timeToLive: 3, 
        data: { 
         "message":"What's up honey?", 
         "title":"Come on" 
        } 
    }); 
    pushAndroidSender.send(message, registrationIds, function(err, result) { 
        if (err) { 
         console.error("this is an error: " + err); 
        } else { 
         console.log("this is a successful result:" + result); 
        } 
    }); 
    
    +0

    我想只有'需要message'。 'title'是可选的 – Ajoy

    +0

    我做了你所说的。 1)在adb logcat中,我可以看到来自gcmservices的一些日志。 2)我的留言数据上有“标题”和“留言”。 3)我设置“delayWhileIdle”为false。但我有同样的问题:( –