当我尝试测试并从HTTP Error 401: Unauthorized
收到通知时,我收到Online GCM Tester
消息。我正在使用this example来实施Google Cloud Messaging
我在以下测试人员上测试了我的应用
我的API密钥:AIzaSyB2u46YIQ22zAgbxUNP4Y0UuQdAUroc6jM
设备令牌ID: eWoZ_ngtrLA:APA91bHC0DEfgYb8QDD3AhGXtuRAv0oW9gUCH1-LvkjRzAlgTTBYbtIEbt1ux2rPylGItXKYL1fGvgRRZhRviFdQ4N1Hp8RFgr2w2g8tqjZw9E8ymcQXRIuKVCjRrwzjUKl1Wmvot-WQ
清单文件
<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.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<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.REGISTRATION" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<service android:name=".GcmService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
GCM LISTNER
public class GcmService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
JSONObject jsonObject = new JSONObject();
Set<String> keys = data.keySet();
for (String key : keys) {
try {
jsonObject.put(key, data.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
主要活动
private void getGCMToken() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
InstanceID instanceID = InstanceID.getInstance(mContext);
String token = instanceID.getToken(SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
if (token != null && !token.isEmpty()) {
SharedPreferences appPrefs = mContext.getSharedPreferences(SHARD_PREF, Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = appPrefs.edit();
prefsEditor.putString(GCM_TOKEN, token);
prefsEditor.apply();
}
配置文件
project_info": {
"project_number": "1005995697870",
"project_id": "default-demo-app-2e614"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1005995697870:android:f36173e948059ccb",
"android_client_info": {
"package_name": "gcm.play.android.samples.com.gcmquickstart"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "AIzaSyB2u46YIQ22zAgbxUNP4Y0UuQdAUroc6jM"
}