【发布时间】:2016-03-23 08:37:39
【问题描述】:
我使用 Eclipse 创建了一个移动应用程序。
服务器通过 GCM(在 PHP 中)发送推送通知。
第一次安装 APK 时,它会发送一个推送通知,该通知工作正常。第二次(同一设备上的同一个APP)发送两次,第三次,三次,依此类推。
我发现问题是由于添加了同一设备的多个 ID 造成的。因此,如果我手动删除所有 ID 并重新安装 APK,它将正常工作。
$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => $registatoin_ids,'data' => $message,); $headers = array( 'Authorization: key=' . 'asbjadbdb','Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) { die('Curl failed: ' . curl_error($ch));}
curl_close($ch);
echo $result;
安卓端
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
sendNotification(false,"Send error: " + extras.toString(),null,null,null);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification(false,"Deleted messages on server: "
+ extras.toString(),null,null,null);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
// sendNotification("Message Received from Google GCM Server:\n\n"
// + extras.get(ApplicationConstants.MSG_KEY));
String name=extras.getString("name");
String address=extras.getString("address");;
sendNotification(true,name,address);
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
【问题讨论】:
-
能否提供发送和接收通知的代码?
-
第一次安装APK时,它会发送一个推送通知,它工作正常。秒数(同一设备上的同一个APP)发送两次,第三次,三次,以此类推。
-
好的,但是通过向我们提供您为发送推送通知而编写的实际代码来帮助我们帮助您
-
@sharsadkk!先给我们看代码。
-
写你的php代码覆盖之前的push id,是一样的。
标签: php android push-notification google-cloud-messaging