解析CorodvaPush Ionic:Android在后台应用时不会显示通知

uks*_*ksz 7 android parse-platform ionic-framework ionic

我的插件有问题.当我在应用程序中,并且我通过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);
Run Code Online (Sandbox Code Playgroud)

谢谢!!

顺便说一句,这是我收到的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"}}
Run Code Online (Sandbox Code Playgroud)

回答:

所以,经过两天拉头发,我终于设法解决了这个问题.所以,问题在于你如何在Parse中指定JSON并不重要,它总是以上面给出的形式发送 - 这意味着你指定的内容总是在payload-> data - >'key':'value'.但是,GCMIntentService寻求通知,该通知在JSON的第一层中具有"消息".意思是,它必须看起来像这样:

 {"collapse_key":"do_not_collapse",
    "event":"message",
    "foreground":true,
    "from":"xxxxxxxxxx",
    "message":"ssadasdasdsa"
......}
Run Code Online (Sandbox Code Playgroud)

您可以看到它在下面的GCMIntentService.java中指定了以"protected void onMessage"开头的行(我认为它的第53行).

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

    @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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我希望这会有所帮助,因为我花了很长时间才想弄明白:).

uks*_*ksz 1

于是,经过两天的折腾,我终于解决了这个问题。因此,问题在于,无论您在 Parse 中如何指定 JSON,它总是以上面提供的形式发送 - 这意味着您指定的内容始终位于 Payload->data->'key':'value' 中。然而,GCMIntentService 寻求一个通知,该通知在 JSON 的第一层中有“消息”。意思是,它必须看起来像这样:

{"collapse_key":"do_not_collapse",
    "event":"message",
    "foreground":true,
    "from":"xxxxxxxxxx",
    "message":"ssadasdasdsa"
......}
Run Code Online (Sandbox Code Playgroud)

您可以看到它是在 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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