简体   繁体   中英

GCM Push notification

I am able to successfully send Push notification from GCM but I am not able to catch message from INTENT data. The message variable is not getting any data.

My 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);

}

My Payload

{
"message": "Hi I am nutritown",
"registration_ids":["registrationId"]
}

Response from GCM {"multicast_id":8633384191969300942,"success":1,"failure":0,"canonical_ids":0,"results": [{"message_id":"0:1420884152153321%b49c80e8f9fd7ecd"}]}

My android manifest file

<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>

The problem was fixed the request was wrong from my part.

Correct request to GCM server should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM