GCM推送通知

时间:2015-01-10 10:12:59

标签: java android push-notification google-cloud-messaging

我能够从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,"成功":1,"失效":0," canonical_ids":0,"结果& #34;:[{" 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>

1 个答案:

答案 0 :(得分:0)

问题得到解决,我的请求是错误的。

对GCM服务器的正确请求应为

{ 
"data": {
"message": "Hi I am message",
"key2": "value2"
},
"registration_ids":["registrationid"]
}