confusion with c2dm messaging in android

瘦欲@ 提交于 2020-01-16 06:25:12

问题


i have implemented a small demo that uses this c2dm code http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html .it works.i am getting push notifications from server but the problem is whenever i send notification from server it sends old notification.It only sends current new notification whenever i register my android device and send registration id to server.Please suggest me what should be the issue.Thanks in advance..


回答1:


As far as I remember from when I followed Lars Vogel's tutorial myself, I had the same problem. It's actually not the problem that you have stated.

Try putting a breakpoint in the onReceive() function to manually see the payload you are getting. In my case, the message was good, but the MessageReceivedActivity was not, and it was always showing the bad message.

Put the super.onCreate() method from the MessageReceivedActivity on top of the method.

How it is:

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

How it should be:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(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);
        }
    }
}



回答2:


Answer from @RaulGogo helped a bit, but did not solve the problem for me. To see this solution here https://stackoverflow.com/a/10079537/264618 and read my comments below.

In general change this code in C2DMMessageReceiver class:

int ukey = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(context, ukey,
            intent, 0);

and add this in MessageReceivedActivity class:

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);//important!

Note: setIntent(intent) call refreshes intent with current data.



来源:https://stackoverflow.com/questions/10135553/confusion-with-c2dm-messaging-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!