【问题标题】:Parse CorodvaPush Ionic: Android doesnt show notifications when app in backgroundParse Corps aPush Ionic:当应用程序在后台运行时,Android 不显示通知
【发布时间】:2015-12-29 19:12:29
【问题描述】:

我的插件有问题。当我在应用程序中并使用 Parse 发送通知时,我会收到一条带有消息的警报(这按预期工作)。但是,当应用程序在后台时,手机上不会显示任何内容。以下是我如何使用插件,以及我如何处理通知:

var gmcId = "xxxxxxx";
var androidConfig = {
  senderID: gmcId
};

document.addEventListener("deviceready", function(){
  $cordovaPush.register(androidConfig).then(function(result) {
    console.log("result: " + result);
  }, function(err) {
    // Error
  })

  $rootScope.$on('$cordovaPush:notificationReceived', function(event, 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.
      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 )
    {


      // if the notification contains a soundname, play it.
      navigator.notification.beep(1);
      alert(e.payload.data.alert)
    }
    else
    {  // otherwise we were launched because the user touched a notification in the notification tray.
      if ( e.coldstart )
      {

      }
      else
      {

        navigator.notification.beep(1);
        alert(e.payload.data.alert)
      }
    }


    break;

  case 'error':
    $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
    break;

  default:
    $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
    break;
}

  });

}, false);

谢谢!!

顺便说一句,这是我收到的 Parse 通知的结构:

{"collapse_key":"do_not_collapse",
"event":"message",
"foreground":true,
"from":"xxxxxxxxxx",
"payload":{
"data":     {"alert":"asdasdas","push_hash":"bff149a0b87f5b0e00d9dd364e9ddaa0"},"push_id":"2iFhVp2R4u","time":"2015-07-21T12:24:09.905Z"}}

回答:

所以,在拉了两天的头发之后,我终于设法解决了这个问题。所以,问题是你如何在 Parse 中指定 JSON 并不重要,它总是以上面提供的形式发送 - 这意味着你指定的总是在有效负载->数据->'key':'value'中。但是,GCMIntentService 会寻找在 JSON 的第一层中具有“消息”的通知。意思是,它必须看起来像这样:

 {"collapse_key":"do_not_collapse",
    "event":"message",
    "foreground":true,
    "from":"xxxxxxxxxx",
    "message":"ssadasdasdsa"
......}

您可以看到它是在 GCMIntentService.java 下面开始的行中指定的 “protected void onMessage”(我认为是第 53 行)。

无论如何,要解决这个问题,我们需要更改通知的处理,当应用程序不在前台时。我们需要将其更改为:

    @Override
protected void onMessage(Context context, Intent intent) {
    Log.d(TAG, "onMessage - context: " + context);

    // Extract the payload from the message
    Bundle extras = intent.getExtras();
    if (extras != null)
    {
        // if we are in the foreground, just surface the payload, else post it to the statusbar
        if (PushPlugin.isInForeground()) {
            extras.putBoolean("foreground", true);
            PushPlugin.sendExtras(extras);
        }
         else {

         try {
               JSONObject object_example = new JSONObject( extras.getString("data"));
                String message = object_example.getString("alert");
                 extras.putString("message", message);
             } catch (JSONException e) {
                 //some exception handler code.
             }

                       createNotification(context, extras);
        }
    }
}

在这里,我们的代码将考虑到我们的通知在 JSON 的第一层中没有“消息” - 它会在 data->alert(来自 PARSE 的通知的标准形式)中寻找它。

我希望这会有所帮助,因为我已经花了很多天的时间试图弄清楚:)。

【问题讨论】:

    标签: android parse-platform ionic-framework ionic


    【解决方案1】:

    所以,在拉了两天的头发之后,我终于设法解决了这个问题。因此,问题在于您如何在 Parse 中指定 JSON 并不重要,它始终以上述形式发送 - 这意味着您指定的内容始终在有效负载->数据->'key':'value'中。但是,GCMIntentService 会寻找在 JSON 的第一层中具有“消息”的通知。意思是,它必须看起来像这样:

    {"collapse_key":"do_not_collapse",
        "event":"message",
        "foreground":true,
        "from":"xxxxxxxxxx",
        "message":"ssadasdasdsa"
    ......}
    

    您可以看到它在 GCMIntentService.java 中以“protected void onMessage”开头的行下方指定(我认为是第 53 行)。

    无论如何,你要解决这个问题,我们需要更改通知的处理,当应用程序不在前台时。我们需要将其更改为:

     @Override
    protected void onMessage(Context context, Intent intent) {
        Log.d(TAG, "onMessage - context: " + context);
    
        // Extract the payload from the message
        Bundle extras = intent.getExtras();
        if (extras != null)
        {
            // if we are in the foreground, just surface the payload, else post it to the statusbar
            if (PushPlugin.isInForeground()) {
                extras.putBoolean("foreground", true);
                PushPlugin.sendExtras(extras);
            }
             else {
    
             try {
                   JSONObject object_example = new JSONObject( extras.getString("data"));
                    String message = object_example.getString("alert");
                     extras.putString("message", message);
                 } catch (JSONException e) {
                     //some exception handler code.
                 }
    
                           createNotification(context, extras);
            }
        }
    }
    

    在这里,我们的代码将考虑到我们的通知在 JSON 的第一层中没有“消息” - 它会在 data->alert(来自 PARSE 的通知的标准形式)中寻找它。

    我希望这会有所帮助,因为我已经花了很多天的时间试图弄清楚:)。

    【讨论】:

      【解决方案2】:

      我使用了一个专门为 Parse.com 推送通知构建的插件,这是我注册通知的方式,它适用于所有场景。

      registerCallback: function (_pushCallback) {
          var deferred = $q.defer();
          $window.parsePlugin.registerCallback('onNotification', function () {
      
              $window.onNotification = function (pnObj) {
      
                  _pushCallback && _pushCallback(pnObj);
      
                  alert('We received this push notification: ' + JSON.stringify(pnObj));
                  if (pnObj.receivedInForeground === false) {
                      // TODO: route the user to the uri in pnObj
                  }
              };
              deferred.resolve(true);
      
          }, function (error) {
              deferred.reject(error);
          });
          return deferred.promise;
      
      }
      

      你可以在这里看到完整的源代码:https://github.com/aaronksaunders/dcww/blob/master/www/js/parseService.js

      使用了这个 Parse 插件 - https://github.com/grrrian/phonegap-parse-plugin

      【讨论】:

      • 我在使用 Parse 插件时遇到了很多问题,因此我想坚持使用 cordovaPush。我对令牌和 userId 的注册部分没有任何问题,我在注册通知时遇到问题 - 我可以通过 alert(notification.payload.data.alert) 收到警报,但是当应用程序在时我无法收到它背景。
      • 好的,好奇您在使用该插件时遇到了什么问题?到目前为止,我们已经在 3 个客户端应用程序中部署了最新版本的分支,没有任何问题,希望您能提供任何提示。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 2019-09-12
      相关资源
      最近更新 更多