Php GCM服务器返回空值
我正在尝试在我的应用程序中实现谷歌云消息。我仍然不明白为什么我的手机没有收到正确的信息。我的服务器发送一条消息,GCM服务器对此做出响应并将消息发送回我的手机Php GCM服务器返回空值,php,android,google-cloud-messaging,Php,Android,Google Cloud Messaging,我正在尝试在我的应用程序中实现谷歌云消息。我仍然不明白为什么我的手机没有收到正确的信息。我的服务器发送一条消息,GCM服务器对此做出响应并将消息发送回我的手机 {\"multicast_id\":8186678237008516542,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1356727074650189%12aaaeccf9fd7ecd\"}]}" 我想这意味着我收到一
{\"multicast_id\":8186678237008516542,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1356727074650189%12aaaeccf9fd7ecd\"}]}"
我想这意味着我收到一条消息,问题是我的应用程序只显示空值。我现在正在使用浏览器Api密钥并得到这些结果,但我尝试使用服务器密钥(理论上更适合我的需要),但我得到了错误401
为了接收信息,我使用广播接收器
public void onReceive(Context context, Intent intent){
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);}
额外消息=消息
这是我在服务器中使用的代码
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
有人知道问题出在哪里吗???从
'data' => $message,
像这样:
'&data.message=' => $message,
GCMinentService中的onMessage()方法应该如下所示:
protected void onMessage(Context ctx, Intent intent) {
// TODO Auto-generated method stub
String message =intent.getStringExtra("message");;
}
我认为您的响应字符串名称不匹配,因此请检查您的响应字符串名称。我在我的服务器端代码和android端代码中都使用了它作为“价格”。你可以在下面的图片中看到它 服务器端的文件:send_message.php 应用程序端的文件:gcmnotificationtentservice
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GCMNotificationIntentService";
@Override
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("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
sendNotification("Message Received from Google GCM Server: "
+ extras.get("price"));
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
//Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
//Log.d(TAG, "Notification sent successfully.");
}
公共静态最终整数通知\u ID=1;
私人通知经理通知经理;
通知建筑商;
公共GCMNotificationtentService(){
超级(“GCMinentService”);
}
公共静态最终字符串TAG=“gcmnotificationtentservice”;
@凌驾
受保护的手部内容无效(意图){
Bundle extras=intent.getExtras();
GoogleCloudMessaging gcm=GoogleCloudMessaging.getInstance(this);
字符串messageType=gcm.getMessageType(intent);
如果(!extras.isEmpty()){
如果(GoogleCloudMessaging.MESSAGE)\类型\发送\错误
.equals(messageType)){
sendNotification(“发送错误:+extras.toString());
}else if(GoogleCloudMessaging.MESSAGE\u TYPE\u已删除
.equals(messageType)){
sendNotification(“服务器上已删除的邮件:”
+附加的toString());
}else if(GoogleCloudMessaging.MESSAGE\u TYPE\u MESSAGE
.equals(messageType)){
对于(int i=0;i<3;i++){
试一试{
睡眠(5000);
}捕捉(中断异常e){
}
}
sendNotification(“从Google GCM服务器收到的消息:”
+额外获得(“价格”);
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
私有void sendNotification(字符串msg){
//Log.d(标记“准备发送通知…”“+msg);
mNotificationManager=(NotificationManager)此
.getSystemService(上下文通知服务);
PendingEvent contentIntent=PendingEvent.getActivity(此,0,
新意图(this,MainActivity.class),0);
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(
此).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle(“GCM通知”)
.setStyle(新通知compat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
//Log.d(标记“通知已成功发送”);
}
您在何处显示空值?空值将返回到模拟器中的android应用程序。您不认为在显示问题的代码或异常的logcat跟踪中包含信息很重要吗?请尝试注销意图的内容,以便查看发送给您的应用程序的内容应用程序通过消息。还要发布onMessage()方法的内容。请确保您在onMessage()中捕捉到了所指出的意图,而不是onReceive()方法。