【问题标题】:Message are recieved from GCM server but could be handled only while application running从 ECM 服务器接收到消息,但只能在应用程序运行时处理
【发布时间】:2013-11-04 03:41:14
【问题描述】:

我运行了一个示例,在我的后端注册了我的设备,该示例通过 GCM 发送回设备消息。问题是我可以在应用程序运行时在 BroadCastreceiver.OnReceive 方法接收这些消息,但如果我退出应用程序并再次发送推送,则不会发生任何事情。

我是否需要额外的实现才能让 Android 系统以原生方式显示推送(如 iOS 和 Windows Phone 那样)。

我遵循了本指南,但没有关于原生 Android 操作系统通知的内容: http://developer.android.com/google/gcm/client.html

ps: 当我在我的客户端应用程序关闭时发送消息时,我仍然收到成功响应:

{"multicast_id":5388850153658301208,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1382683020113430%1a3104c7f9fd7ecd"}]}

感谢您的任何建议!

更新 1: 清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          android:installLocation="auto" 
          package="com.pushtest.droid" 
          android:versionCode="1" 
          android:versionName="1">

  <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />

  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />

  <permission android:name="com.pushtest.droid.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
  <uses-permission android:name="com.pushtest.droid.permission.C2D_MESSAGE" />

  <application android:label="push droid">
    <receiver android:name="com.pushtest.droid.MyGcmBroadcastReceiver"
              android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <action android:name="com.google.android.gcm.intent.RETRY" />

        <category android:name="com.pushtest.droid" />
      </intent-filter>
    </receiver>
  </application>
</manifest>

【问题讨论】:

  • 能否请您发布您的代码和清单文件。
  • 刚刚添加。正如我在注册设备并在应用程序运行时交付推送之前所说的那样,所以我认为清单没有问题。我想我缺少对 GCM 的一些概念理解
  • 无论您的应用程序是否运行,您都会收到来自 GCM 的消息。但是您可以在收到消息后处理下一个流程。
  • 你在清单文件中哪里声明了 GCMIntentService ???
  • 应用不运行时是否需要接收推送?我试图评论它并在没有它的情况下运行我的推送处理(假设应用程序未运行时系统将自行处理)

标签: android push-notification broadcastreceiver google-cloud-messaging intentservice


【解决方案1】:

最终我错过了在 Android 中您必须自己显示 PUSH 通知这一事实。 所以我已经实现了如下处理逻辑,现在即使没有 IntentService,我也可以在推送到达时看到我的通知(对不起语法,它是 monodroid,但 java 中的概念是相同的):

private void HandleMessage(Context context, Intent intent)
{
   var extras = intent.Extras;
   string message = extras.GetString("message");
   string title = extras.GetString("title");
   var notif = new Notification(Resource.Drawable.Icon, title);
   notif.Flags |= NotificationFlags.AutoCancel;
   notif.Defaults = NotificationDefaults.Sound | NotificationDefaults.Vibrate;
   var notificationIntent = new Intent(context, typeof(Activity1));
   notificationIntent.SetFlags(ActivityFlags.SingleTop);
   var contentIntent = PendingIntent.GetActivity(context, 0, notificationIntent, 0);
   notif.SetLatestEventInfo(context, title, message, contentIntent);
   var mNotificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
   mNotificationManager.Notify(1, notif);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2015-12-09
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多