推送通知中的消息未刷新

时间:2012-03-13 15:04:59

标签: android

我的问题是我能够从服务器收到通知,但是来自服务器的消息并没有在我的手机中刷新,无论第一次出现同样的消息,我会一直得到这些消息。这是我的C2DMMessageReceiver&的代码。 MessageReceivedActivity如下。

   public class C2DMMessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.w("C2DM", "Message Receiver called");
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
            Log.w("C2DM", "Received message");
            final String payload = intent.getStringExtra("payload");
            createNotification(context, payload);
        }
    } 
    public void createNotification(Context context, String payload) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.icon,
                "Message received", 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);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                intent, 0);
        notification.setLatestEventInfo(context, "Message",
                "New message received", pendingIntent);
        notificationManager.notify(0, notification);

    }

}

public class MessageReceivedActivity extends Activity 
 {

@Override

protected void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.activity_result);

Bundle extras = getIntent().getExtras();

if (extras != null) {

String message = extras.getString("payload");


if (message != null && message.length() > 0) {

TextView view = (TextView) findViewById(R.id.result);

view.setText(message);

}

}

super.onCreate(savedInstanceState);

}
}

2 个答案:

答案 0 :(得分:0)

我可以看到你的问题是缓存......

例如在我使用php的服务器中我在发送输出之前使用此标题:

    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");

希望这能帮到你

最好的问候, 范哲利斯

答案 1 :(得分:0)

因为我看到你需要在你的活动中注册C2DMMessageReceiver尝试覆盖onResume()方法并在这个Intent“com.google.android.c2dm.intent.RECEIVE”上注册Receiver。