2014-05-24 30 views
0

我有一個問題從我的google開發者帳戶獲取令牌。 從我的類GcmIntentService onRegistered的GCM方法不叫......Android GCM onRegistered未調用

這是我的源代碼:

的Manifest.xml:

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

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

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

    <permission 
     android:name="com.test.gcm.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.test.gcm.permission.C2D_MESSAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.test.gcm.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver 
      android:name=".GcmBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 

       <category android:name="com.test.gcm" /> 
      </intent-filter> 
     </receiver> 

     <service android:name=".GcmIntentService" /> 
    </application> 

</manifest> 

和我GcmIntentService.java:

package com.test.gcm; 

import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 

import com.google.android.gcm.GCMBaseIntentService; 

public class GcmIntentService extends GCMBaseIntentService { 
    public static String TAG = "GCMIntentService"; 
    public GcmIntentService(String senderId) { 
     super("************"); 
     Log.d("GCMIntentService", senderId); 
    } 

    @Override 
    protected void onError(Context arg0, String arg1) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    protected void onMessage(Context arg0, Intent arg1) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    protected void onRegistered(Context arg0, String arg1) { 
     // TODO Auto-generated method stub 
     Log.d("token", arg1); 
    } 

    @Override 
    protected void onUnregistered(Context arg0, String arg1) { 
     // TODO Auto-generated method stub 

    } 

} 

謝謝。

回答

0

onRegistered未被調用,因爲您的接收方的意圖過濾器中沒有com.google.android.c2dm.intent.REGISTRATION

它應該是:

<receiver 
     android:name=".GcmBroadcastReceiver" 
     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" /> 
      <category android:name="com.test.gcm" /> 
     </intent-filter> 
    </receiver> 

這assuing您正在使用舊的註冊方法(GCMRegistrar.register)。

如果您使用的是新的登記方法(GoogleCloudMessaging.register),你不需要onReceive的方法,因爲當你調用register你會得到的註冊ID同步。

0

好吧,我已經添加了這一行,但這並沒有解決我的問題:/。如果可能的話,我會成功獲得帶有此解決方案的令牌。 如果我在調用GCMRegistrar.register方法時明白onRegistered方法是調用?

這是我的新清單:

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

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

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

    <permission 
     android:name="com.test.gcm.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.test.gcm.permission.C2D_MESSAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.test.gcm.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver 
      android:name="com.google.android.gcm.GCMBroadcastReceiver" 
      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" /> 
       <category android:name="com.test.gcm" /> 
      </intent-filter> 
     </receiver> 

     <service android:name=".GcmIntentService" /> 
    </application> 

</manifest> 

感謝。