2015-11-20 70 views
-1

我遵循developers.google.com的示例,並創建簡單的服務以從GCM中檢索令牌和消息。GCM Android 4.0.4無法獲取消息

級令牌收到

public class RegistrationIntentService extends IntentService { 

    private static final String TAG = "RegIntentService"; 

    private static final String[] TOPICS = {"global"}; 

    private String projectNumber = "gcm-test-xxxxx"; 

    public RegistrationIntentService() { 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 

     try { 

      InstanceID instanceID = InstanceID.getInstance(this); 
      String token = instanceID.getToken(projectNumber, 
        GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 

      sendRegistrationToServer(token); 
      subscribeTopics(token); 
      sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply(); 


     } catch (IOException e) { 
      Log.d(TAG, "Failed to complete token refresh", e); 
      sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply(); 
     } 
     Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); 
    } 

    private void subscribeTopics(String token) { 

    } 

    private void sendRegistrationToServer(String token) throws IOException { 
     GcmPubSub pubSub = GcmPubSub.getInstance(this); 
     for (String topic : TOPICS) { 
      pubSub.subscribe(token, "/topics/" + topic, null); 
     } 
    } 

} 

這個類的工作很好,我可以檢索令牌,做我想做的事情。 我從MainActivity

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     startService(new Intent(this, RegistrationIntentService.class)); 
    } 
} 

開始這個類的服務我也有類從GCM檢索消息。這個班根本不工作。

public class MyGcmListenerService extends GcmListenerService { 
    String TAG = "MyGcmListenerService"; 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("message"); 
     Log.d(TAG, "From: " + from); 
     Log.d(TAG, "Message: " + message); 
     }  
} 

所有這些東西都在清單中註冊。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="gcm_test.app"> 


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


    <application 
      android:allowBackup="true" 
      android:icon="@mipmap/ic_launcher" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme"> 
     <activity 
       android:name=".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.gms.gcm.GcmReceiver" 
       android:exported="true" 
       android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <category android:name="gcm_test.app" /> 
      </intent-filter> 
     </receiver> 

     <service 
       android:name=".gcm.MyGcmListenerService" 
       android:exported="false"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE"/> 
      </intent-filter> 
     </service> 

     <service 
       android:name=".gcm.RegistrationIntentService" 
       android:exported="false"> 
     </service> 

    </application> 

</manifest> 

我發送消息與郵遞員。

POST https://android.googleapis.com/gcm/send 
headers 
Content-Type:application/json 
Authorization:key= keyFromGoogleDevelopersConsole 
{ 
    "to" : "/topics/global", 
    "data":{ 
     "message": "Hello world!!!" 
    } 
} 

發送後我收到200 OK和消息ID,但手機沒有收到任何消息。

我在做什麼是錯誤的?如何收到我的消息?

我已將SenderID更改爲Developers Console中的號碼,但它沒有幫助我。在調試控制檯ASLO我注意到錯誤:

11-21 17:32:58.014 31813-31813/gcm_test.app E/dalvikvm﹕ Could not find class 'android.app.Notification$BigTextStyle', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza 
11-21 17:32:58.024 31813-31813/gcm_test.app E/dalvikvm﹕ Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzap 
11-21 17:32:58.027 31813-31813/gcm_test.app E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzb 

我認爲這服務,retrives的消息需要以某種方式登記或啓動。但是當我像簡單的服務一樣運行時,它崩潰了。

+0

如果您查看logc如果輸出中沒有過濾器,那麼您希望收到消息時,您將看到如下所示的行:「I/GCM:GCM消息gcm_test.app 0:1448134660868753%04d3815af9fd7ecd」,如果消息是由您的設備接收的。如果你看到這個,但是你的GcmListenerService沒有被調用,那麼你知道你已經成功註冊了GCM,但是這個消息沒有通過你的服務。 –

回答

0

看起來您沒有註冊正確的發件人ID。 documentation解釋了發件人ID是Google Developers Console中爲您的應用項目列出的數字項目編號。您用於發件人ID的字符串gcm-test-xxxxx看起來像項目ID。

在在開發者控制檯儀表盤頁面,您的項目數據將包括這樣一行:

ID: jade-segment-123 (#123123123123) 

在這個例子中,發件人ID爲123123123123.

更新: 注此外您還沒有使用以下網址中顯示的網址:documentation

https://gcm-http.googleapis.com/gcm/send 
+0

你好,你從一開始就是對的。我設置了錯誤的發件人ID。但是當我改變它時,我沒有看到這些消息,因爲我將過濾器應用於具有信息級別的消息的IDEA控制檯。所以愚蠢的錯誤=( 我沒有測試這個網址https://gcm-http.googleapis.com/gcm/send,與https://android.googleapis.com/gcm/send正常工作。 非常感謝您的幫助。 –