gau*_*anu 0 android push-notification google-cloud-messaging
我在接收谷歌推送通知时面临以下问题.我从PHP提交以下通知,并在android GCMBaseIntentService中添加另一个标签data.notificationID以获取接收唯一ID.
$data = array(
'registration_id' => $deviceToken,
'collapse_key' => $collapseKey,
'data.message' => $messageText ,
'data.notificationID' => $yourKey
);
Run Code Online (Sandbox Code Playgroud)
通知正常,但notifyID值较旧,我先推了.它不是链接.但是我发送的data.message消息字符串是新来的.
php中的推送通知代码是:
<?php
$Key = "0000000";
$deviceToken = "sdfsjdfljsd";
$collapseKey = 1;
$messageText = "Hi welcome";
//$yourKey = 100;
$yourKey = 101;
$headers = array('Authorization:key=' . $Key);
$data = array(
'registration_id' => $deviceToken,
'collapse_key' => $collapseKey,
'data.message' => $messageText ,
'data.notificationID' => $yourKey
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
return false;
}
if ($httpCode != 200) {
return false;
}
curl_close($ch);
?>
Run Code Online (Sandbox Code Playgroud)
并在Android GCMIntentService实现中:
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
@SuppressWarnings("hiding")
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super("XXXXXXXXXX");
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message , String notificationID ) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, ViewNotification.class);
//notificationIntent.removeExtra("notificationID");
notificationIntent.putExtra("notificationID", notificationID);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 1000;
notification.ledOffMS = 300;
notificationManager.notify( 1 , notification);
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.d("GCM", "RECIEVED A MESSAGE");
generateNotification(arg0, arg1.getStringExtra("message") , arg1.getStringExtra("notificationID") );
//generateNotification(arg0, arg1.getStringExtra("key1"));
}
@Override
protected void onRegistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
}
Run Code Online (Sandbox Code Playgroud)
在下面这行我变老了有效的notificationID,但每次我提交新的ID.
generateNotification(arg0,arg1.getStringExtra("message"),arg1.getStringExtra("notificationID"));
如果有的话,请帮我查找bug.
这是一个常见问题,挂起的意图没有更新,只需改变如下:
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)