我遵循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的消息需要以某種方式登記或啓動。但是當我像簡單的服務一樣運行時,它崩潰了。
如果您查看logc如果輸出中沒有過濾器,那麼您希望收到消息時,您將看到如下所示的行:「I/GCM:GCM消息gcm_test.app 0:1448134660868753%04d3815af9fd7ecd」,如果消息是由您的設備接收的。如果你看到這個,但是你的GcmListenerService沒有被調用,那麼你知道你已經成功註冊了GCM,但是這個消息沒有通過你的服務。 –