【问题标题】:GCM Push notificationGCM 推送通知
【发布时间】:2015-03-08 15:04:16
【问题描述】:

我能够从 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"}]}

我的安卓清单文件

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

【问题讨论】:

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


    【解决方案1】:

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

    对 GCM 服务器的正确请求应该是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 2012-11-27
      相关资源
      最近更新 更多