GCM 推送通知
GCM Push notification
我能够从 GCM 成功发送推送通知,但我无法从 INTENT 数据中捕获消息。
消息变量未获取任何数据。
我的 GCMIntentService class
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"]
}
我能够从 GCM 成功发送推送通知,但我无法从 INTENT 数据中捕获消息。 消息变量未获取任何数据。
我的 GCMIntentService class
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"]
}