【问题标题】:Receiving Messages Sent Through GCM in Android Version 4.0.4在 Android 版本 4.0.4 中接收通过 GCM 发送的消息
【发布时间】:2013-12-10 15:26:51
【问题描述】:

通过http发送消息后。我无法在 android 设备版本 4.0.4 上接收消息。它在启用调试但意图具有操作 com.google.android.c2dm.intent.REGISTRATION 时触发 OnReceive() 方法,它应该是 com.google.android.c2dm.intent.RECEIVE。 所以我无法收到消息。让所有版本正常工作。

我的 Android.Manifest 文件代码是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="hellomultiscreen" android:versionCode="11" android:versionName="2.2.0" >
  <uses-sdk android:minSdkVersion="8" />
  <uses-permission android:name="android.permission.INTERNET" />
  <permission android:name="hellomultiscreen.permission.C2D_MESSAGE" android:protectionLevel="signature" />
  <uses-permission android:name="hellomultiscreen.permission.C2D_MESSAGE" />
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  <application android:label="Online Booking" >

    <!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it -->
        <receiver android:name="hellomultiscreen.MyGCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
            <!-- Receive the actual message -->
            <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.c2dm.intent.RETRY" />
                <category android:name="hellomultiscreen" />
            </intent-filter>

        </receiver>
    <service android:name="hellomultiscreen.MyIntentService" enabled="true" />
    </application>
    <uses-sdk />
</manifest>

以及通过 GCM 发送消息后成功触发 OnReceive() 方法的 BroadCastReceiver 文件是(此处获取 Intent-Action 注册而不是 RECEIVE)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace hellomultiscreen
{

    public class MyGCMBroadcastReceiver : BroadcastReceiver
    {

        const string TAG = "PushHandlerBroadcastReceiver";
        public override void OnReceive(Context context, Intent intent)
        {

            MyIntentService.RunIntentInService(context, intent);
            SetResult(Result.Ok, null, null);
            Toast.MakeText(context, "Received Message!", ToastLength.Short).Show();
        }
    }
}

【问题讨论】:

    标签: android android-intent google-cloud-messaging


    【解决方案1】:

    试试这个接收器

    public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // Explicitly specify that GcmIntentService will handle the intent.
            ComponentName comp = new ComponentName(context.getPackageName(),
                    GcmIntentService.class.getName());
            // Start the service, keeping the device awake while it is launching.
            startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-06
      • 2014-04-24
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      相关资源
      最近更新 更多