android - 解析 CorodvaPush Ionic : Android doesnt show notifications when app in background

标签 android parse-platform ionic-framework

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

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 并不重要,它总是以上面呈现的形式发送 - 这意味着你指定的内容总是在 payload->data->'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 的第一层中没有“消息”——它将在数据->警报(来自 PARSE 的通知的标准形式)中寻找它。

我希望这对您有所帮助,因为我已经花了很多天时间试图弄明白 :)。

最佳答案

所以,经过两天的努力,我终于设法解决了这个问题。所以,问题是你如何在 Parse 中指定 JSON 并不重要,它总是以上面呈现的形式发送 - 这意味着你指定的内容总是在 payload->data->'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 的第一层中没有“消息”——它将在数据->警报(来自 PARSE 的通知的标准形式)中寻找它。

我希望这对您有所帮助,因为我已经花了很多天时间试图弄明白 :)。

关于android - 解析 CorodvaPush Ionic : Android doesnt show notifications when app in background,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31431135/

相关文章:

android - Activity 的 onDestroy() 总是会在应用程序销毁时被调用吗?

java - 在Android中运行 Espresso 测试时出现内存不足的问题

android - Jetpack 撰写 : Not able to show text in TextField

ios - 使用 Parse 查询效率

ios - 从Parse.com流音频-没有检索到数据?

ios - Ionic 4 在 Android 上工作时未在 IOS 设备上打开外部/默认电子邮件应用程序

android - 在安装我的应用程序期间,我如何要求用户安装另一个 Android 应用程序(我的应用程序依赖于该应用程序)?

ios - 上传 DSYM 文件后解析未符号化的崩溃分析

ios - 如何将 Ubuntu 上的 Ionic 项目导出到 iOS?

Angular 10/Ionic 5 - 将输入数据从模态传递到父组件