首次运行后的Android C2DM(推送通知)

时间:2012-04-09 12:35:23

标签: android notifications message android-c2dm notificationmanager

我尝试发送推送通知;一切似乎都很好,但是 我有一个问题是在首次运行后将有效负载传递给通知

这是我的代码。

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);
            }
        }
    }

我发送给第二个活动的消息永远不会更新!!

0 个答案:

没有答案