我尝试发送推送通知;一切似乎都很好,但是我在第一次运行后将有效负载传递给通知时遇到了问题。
这是我的代码。
public void createNotification(Context context, String payload) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"Received message", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, MessageReceivedActivity.class);
intent.putExtra("payload", payload);
Log.d("C2DM", "OK.. TRUE MESSAGE --->: payload = " + payload);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, "news",
"news...", pendingIntent);
notificationManager.notify(0, notification);
}
..之后,当我阅读通知时:
public class MessageReceivedActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_result);
super.onCreate(savedInstanceState);
}
@Override
protected void onStart() {
super.onStart();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String message = extras.getString("payload");
Log.d("C2DM", "ERROR: ALWAYS FIRST MESSAGE!! --> " + message);
if (message != null && message.length() > 0) {
TextView view = (TextView) findViewById(R.id.result);
view.setText(message);
}
}
}
我发送给第二个活动的消息永远不会更新!