android - 我无法打开通知作为第一个通知

标签 android android-intent textview google-cloud-messaging

我有一个关于推送通知的问题,我可以处理发送消息并在 TextView 中显示它。但在那之后我发送了第二个推送并尝试打开它,它仍然显示上一个推送通知的消息。

我该如何解决?

这是代码句柄消息

  package com.example.cepyolwebview;

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;

    import com.xtify.sdk.api.XtifyBroadcastReceiver;
    import com.xtify.sdk.api.XtifySDK;

    public class XtifyNotifier extends XtifyBroadcastReceiver {
        String TAG = XtifyNotifier.class.getName();
        private static final String NOTIFICATION_TITLE = "com.xtify.sdk.NOTIFICATION_TITLE";
        private static final String NOTIFICATION_CONTENT = "com.xtify.sdk.NOTIFICATION_CONTENT";

        @Override
        public void onMessage(Context context, Bundle msgExtras) {
            Log.d(TAG, "-- Notification recived");
            String title = msgExtras.getString(NOTIFICATION_TITLE);
            String message = msgExtras.getString(NOTIFICATION_CONTENT);
            Log.d(TAG, "Notification Title: " + title);
            Log.d(TAG, "Notification Content: " + message);
            generateNotification(context, title, message);

        }

        @Override
        public void onRegistered(Context context) {
            Log.d(TAG, "-- SDK registerd");
            Log.d(TAG, "XID is: " + XtifySDK.getXidKey(context));
        }

        @Override
        public void onC2dmError(Context context, String errorId) {
            Log.i(TAG, "ErrorId: " + errorId);
        }

        private static void generateNotification(Context context, String title,
                String message) {
            int icon = R.drawable.ic_launcher;

            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(icon, title,
                    System.currentTimeMillis());

            Intent notificationIntent = new Intent(context, Utils.class);
            // set intent so it does not start a new activity
            notificationIntent.putExtra("message", message);
            notificationIntent.putExtra("title", title);

            PendingIntent intent = PendingIntent.getActivity(context, 0,
                    notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, notification);
        }
  }

和查看通知的代码

package com.example.cepyolwebview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Utils extends Activity {

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.utils);
        TextView title= (TextView) findViewById(R.id.title);
        TextView message= (TextView) findViewById(R.id.message);

        title.setText(getIntent().getExtras().getString("title"));
        message.setText(getIntent().getExtras().getString("message"));


    }
}

最佳答案

使用这个。

 PendingIntent intent=PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT );

关于android - 我无法打开通知作为第一个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12281788/

相关文章:

Android studio Bumblebee build.gradle 根项目无法添加类路径依赖

java - 找不到引用的方法 'com.google.firebase.tasks.Task setValue(java.lang.Object,java.lang.Object)'

android - 我们可以通过 intent 在 android 中传输多少数据?

java - 在不同的Activity中动态添加TextView(RelativeLayout)

android - 如何将数字放入多个 TextView 中?

java - ArrayList 到 DavidWebb Android

android - 如何使用中性按钮创建自定义首选项?

java - Android textview 不显示正确的字符

android - 单击谷歌地图信息窗口时调用另一个 fragment 页面

android - 实现自 api 17 以来已弃用的滑动抽屉的替代方法