[英]GCM Push notification
我能夠從GCM成功發送推送通知,但無法從INTENT數據中捕獲消息。 消息變量未獲取任何數據。
我的GCMIntentService類別
protected void onMessage(Context context, Intent data) {
String message;
message = data.getExtras().getString("message");
Log.i("message",data.toString());
Intent intent = new Intent(this, GCMMessageView.class);
intent.putExtra("message", message);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle("Android GCM Tutorial")
.setContentText(message).setContentIntent(pIntent)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(R.string.app_name, notification);
}
我的有效載荷
{
"message": "Hi I am nutritown",
"registration_ids":["registrationId"]
}
來自GCM {“ multicast_id”:8633384191969300942,“ success”:1,“ failure”:0,“ canonical_ids”:0,“ results”:[{“ message_id”:“ 0:1420884152153321%b49c80e8f9fd7ecd”}]的響應
我的Android清單文件
<permission android:name="com.androidbegin.gcmtutorial.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.androidbegin.gcmtutorial.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".GCMMainActivity"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GCMMessageView" >
</activity>
<service android:name=".GCMIntentService" />
<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.androidbegin.gcmtutorial" />
</intent-filter>
</receiver>
</application>
問題已解決,我的請求是錯誤的。
對GCM服務器的正確請求應為
{
"data": {
"message": "Hi I am message",
"key2": "value2"
},
"registration_ids":["registrationid"]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.