当应用程序在后台时,ngCordova / Ionic推送通知

时间:2015-02-12 00:12:22

标签: push-notification ionic-framework pushsharp phonegap-pushplugin ngcordova

我目前正在使用ionic / ngcordova构建一个Android应用程序。我正在实施推送通知。我已将推送通知实现为在app.run(function(){..})阶段注入的服务。注册部分工作,我收到一个包含regid的回调。此外,当应用程序处于活动状态时,将引发事件并收到通知。

我遇到的问题是,当应用程序进入后台时,根本不会收到通知。我希望当应用程序没有运行或类似的东西时会引发本地通知,但绝对没有任何反应,这很奇怪。

我最近几天在网上搜寻寻找解决方案,但我一直无法找到任何能告诉我它应该工作的东西。

以下是我的app中的notificationService.js

app.factory('notificationService', ['$cordovaPush', function($cordovaPush){

  var dataFactory = {};

  //
  // When the device is ready and this service has been plumbed in...
  document.addEventListener("deviceready", function(){

      console.log("initializing push notifications...");

      _register();

  }, false);


  //
  // Registers the device for push notifications...
  var _register = function(){

      var config = {};

      if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){

          // TODO: centralise this value as it can change...
          config = {
              senderID: "448168747432",
              ecb: "onNotificationGCM"
          };

      }else {
          // iOS
          config = {
              "badge":"true",
              "sound":"true",
              "alert":"true"
          };

          // Can add the following property to the config object to raise a callback with the information if need be...
          // "ecb": "onNotificationRegisterAPN"
      }

      $cordovaPush.register(config).then(function(result){

                    //
                    // Typically returns "ok" for android and devicetoken for iOS
          console.log(result);

      });
  };

    window.onNotificationGCM = function(result){

        console.log(result);

        /*
         I get called when the app is in the foreground, but nothing happens when the app is in the background.
        */

    };

  dataFactory.register = _register;

  return dataFactory;
}]);

如果有帮助,我通过.net应用程序使用PushSharp来发送通知。任何帮助将不胜感激。

更新:我正在使用以下框架/库:

  • Ionic Framework 1.2.14-beta6
  • Cordova 4.2.0
  • PushPlugin

1 个答案:

答案 0 :(得分:3)

对于那些像我一样长时间拔头发的人来说,解决方案非常简单......我在Pushsharp QueueNotification请求中缺少两个属性。所以使用PushSharp github repo上给出的示例:https://github.com/Redth/PushSharp#sample-code

push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE-REGISTRATION-ID-HERE").WithJson("{\"alert\":\"Hello World!\",\"badge\":7,\"sound\":\"sound.caf\"}"));

需要更新以添加缺少的属性:

push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE REGISTRATION ID HERE")
                      .WithJson(@"{""alert"":""This is the future"",""badge"":7,""sound"":""sound.caf"",""title"":""Status Bar title"",""message"":""Some text you want to display to the user""}"));

否则,如果您的应用程序恰好使用Cordova开发而且当前不在前台,那么什么都不会重复,不会发生任何事情。

请将我的帽子提交给gdelavald,并附上他对PushPlugin的评论,指出我在正确的方向:

https://github.com/phonegap-build/PushPlugin/issues/212