草庐IT

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

coder 2023-11-27 原文

我的插件有问题。当我在应用程序中并使用 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 - 解析 CorodvaPush Ionic : Android doesnt show notifications when app in background的更多相关文章

  1. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  4. ruby-on-rails - 我更新了 ruby​​ gems,现在到处都收到解析树错误和弃用警告! - 2

    简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und

  5. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  6. ruby - 用 YAML.load 解析 json 安全吗? - 2

    我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("

  7. ruby - 如何使用 Nokogiri 解析纯 HTML 表格? - 2

    我想用Nokogiri解析HTML页面。页面的一部分有一个表,它没有使用任何特定的ID。是否可以提取如下内容:Today,3,455,34Today,1,1300,3664Today,10,100000,3444,Yesterday,3454,5656,3Yesterday,3545,1000,10Yesterday,3411,36223,15来自这个HTML:TodayYesterdayQntySizeLengthLengthSizeQnty345534345456563113003664354510001010100000344434113622315

  8. python - 帮我找到合适的 ruby​​/python 解析器生成器 - 2

    我使用的第一个解析器生成器是Parse::RecDescent,它的指南/教程很棒,但它最有用的功能是它的调试工具,特别是tracing功能(通过将$RD_TRACE设置为1来激活)。我正在寻找可以帮助您调试其规则的解析器生成器。问题是,它必须用python或ruby​​编写,并且具有详细模式/跟踪模式或非常有用的调试技术。有人知道这样的解析器生成器吗?编辑:当我说调试时,我并不是指调试python或ruby​​。我指的是调试解析器生成器,查看它在每一步都在做什么,查看它正在读取的每个字符,它试图匹配的规则。希望你明白这一点。赏金编辑:要赢得赏金,请展示一个解析器生成器框架,并说明它的

  9. ruby - 如何用 Nokogiri 解析连续的标签? - 2

    我有这样的HTML代码:Label1Value1Label2Value2...我的代码不起作用。doc.css("first").eachdo|item|label=item.css("dt")value=item.css("dd")end显示所有首先标记,然后标记标签,我需要“标签:值” 最佳答案 首先,您的HTML应该有和中的元素:Label1Value1Label2Value2...但这不会改变您解析它的方式。你想找到s并遍历它们,然后在每个你可以使用next_element得到;像这样:doc=Nokogiri::HTML(

  10. ruby-on-rails - 如何在 Rails 3 中禁用 XML 解析 - 2

    我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::

随机推荐