2013-12-10 86 views
0

通過http發送消息後。我無法在Android設備版本4.0.4上收到消息。它在啓用調試時觸發了OnReceive()方法,但意圖使用com.google.android.c2dm.intent.REGISTRATION操作,它應該是com.google.android.c2dm.intent.RECEIVE。 所以我無法獲得消息。保持所有版本的正常工作。通過Android版本4.0.4中的GCM發送接收消息

我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> 

,並在那裏的onReceive()方法成功與發送消息thorugh GCM後發射廣播接收器文件是(及彼意向登記行動,而不是接收)

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(); 
     } 
    } 
} 

回答

0

試試這款接收器

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); 
    } 
}