即时通讯使用谷歌云消息传递,我成功发送推送通知。我唯一的问题是onMessageReceived方法总是为空。 我可以通过日志告诉它,它显示为null。我从服务器发送给用户,这就是我得到的
例如,
04-16 12:29:17.231 19637-19705 / com.world.bolandian.watchme
D / MyGcmListenerService:消息:null
这是我的服务器代码
package jdbc;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.sound.midi.SysexMessage;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
public class GcmSender {
private static final int GCM_RETRIES = 2;
public void sendGooglePushNotification(String token, String type, String data, String serverApiKey) {
// Create Message
Message.Builder builder = new Message.Builder().delayWhileIdle(true);
try {
builder.addData(Params.TYPE_MESSAGE, URLEncoder.encode(type, "UTF-8"));
builder.addData(Params.DATA_MESSAGE, URLEncoder.encode(data, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Message message = builder.build();
// Create Sender
Sender sender = new Sender(serverApiKey);
// Sending Message with Sender to the list of users.
try {
Result rs=sender.send(message, token, GCM_RETRIES);
String ans=rs.toString();
System.out.println(ans);
//System.out.println("notification sent");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// Failed to Send Message.
}
}
}
这是Android代码
package com.world.bolandian.watchme;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GcmListenerService;
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// [START_EXCLUDE]
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/
/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
// [END_EXCLUDE]
}
// [END receive_message]
/**
* Create and show a simple notification containing the received GCM message.
*
* @param message GCM message received.
*/
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("ALERT")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
我确实收到了GCM消息但没有从服务器发送的消息(数据)。并在日志上显示我null。 你们有任何想法如何解决这个问题吗?
答案 0 :(得分:1)
Message.Builder builder = new Message.Builder().addData("message", "Your string here").delayWhileIdle(true);
OR
Message message = new Message.Builder().addData("message", "Your string here").delayWhileIdle(true).build();
在服务器端,您需要向消息中添加数据,您应该使用键和值添加数据,然后您将在Android中获取它,在您的代码中等待来自键的数据" message"在android中虽然没有从服务器发送它