我已经通过JSP从服务器端编写了代码,它可以将消息发送到我的应用程序。 我的应用程序上有通知。
但是,内容丢失了。 以下是服务器端代码:
String message = "This is a test message.";
String registrationId="xxxxxx";
Sender sender = new Sender("My API key");
Message gcmMessage = new Message.Builder().addData(registrationId,message).build();
Result result=sender.send(gcmMessage, registrationId, 5);
out.println(result.toString());
以下是我收到消息的应用代码部分:
protected void onMessage(Context context, Intent data) {
String message;
// Message from PHP server
message = data.getStringExtra("message");
// Open a new activity called GCMMessageView
Intent intent = new Intent(this, GCMMessageView.class);
// Pass data to the new activity
//test
Log.v("sky", "MESSAGE RECEIVED : "+data.getExtras().toString());
Log.v("sky","Message:"+message);
intent.putExtra("message", message);
// Starts the activity on notification click
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the notification with a notification builder
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle("Android GCM Tutorial")
.setContentText(message).setContentIntent(pIntent)
.getNotification();
// Remove the notification on click
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(R.string.app_name, notification);
{
// Wake Android Device when notification received
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
final PowerManager.WakeLock mWakelock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
mWakelock.acquire();
// Timer before putting Android Device to sleep mode.
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
mWakelock.release();
}
};
timer.schedule(task, 5000);
}
}
在我的data.getExtras().toString());
部分,我的日志中有文字,似乎无法从结果中提取消息:
04-01 19:16:45.951: V/sky(21451): MESSAGE RECEIVED : Bundle[{xx My Register ID xxx=This is a test message., collapse_key=do_not_collapse, from=584034591331}]
我可以知道如何获取消息吗?谢谢!
答案 0 :(得分:0)
这是你的错误:
Message gcmMessage = new Message.Builder().addData(registrationId,message).build();
应该是:
Message gcmMessage = new Message.Builder().addData("message",message).build();
您是通过名称为注册ID的参数发送邮件内容的,这是没有意义的。