【问题标题】:When notifications is opened it displays content of the previous notification打开通知时,它会显示上一个通知的内容
【发布时间】:2015-11-19 15:37:55
【问题描述】:

我在 Android 中有一个显示最新消息的应用。当有重大故事或突发新闻时,我会使用 GCM 向应用程序发送通知。但我有一个问题。这个问题的场景是这样的:

  • 我为第一条新闻发送通知
  • 用户不想打开它,所以他刷掉了它
  • 一段时间后,另一个消息发送另一个通知,与第一个不同
  • 当用户打开它时,它会显示第一条新闻,而不是第二条,而是正确的。

我使用的代码如下:

public class GCMNotificationIntentService extends IntentService {
    // Sets an ID for the notification, so it can be updated
    public static final int notifyID = 9001;
    JSONArray array_add = null;
    NotificationCompat.Builder builder;

    // object helper to store the notification configuration
    private SharedPreferenceNotification shared_pref;

    String type, content, nr, title, link, subtype;

    public GCMNotificationIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                    .equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                    .equals(messageType)) {
                sendNotification("Deleted messages on server: "
                        + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                    .equals(messageType)) {
                sendNotification("[" + extras.get(ApplicationConstants.MSG_KEY)
                        + "]");
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg) {


        try {
            array_add = new JSONArray(msg);

            try {
                for (int i = 0; i < array_add.length(); i++) {

                    JSONObject c = array_add.getJSONObject(i);
                    type = c.getString(Utils.NOT_TYPE);

                    content = "[" + c.getString(Utils.NOT_CONTENT) + "]";

                    JSONArray content_a = new JSONArray(content);
                    for (int j = 0; j < content_a.length(); j++) {

                        JSONObject b = content_a.getJSONObject(j);
                        title = b.getString(Utils.NOT_TITLE);
                        nr = b.getString(Utils.NOT_NR);
                        link = b.getString(Utils.NOT_LINK);
                        subtype = b.getString(Utils.NOT_SUBTYPE);
                    }

                }
            } catch (Exception e) {

            }
        } catch (Exception e) {

        }

        Tracker t = GoogleAnalytics.getInstance(this)
                .newTracker("UA-2983962-14");

        // Build and send an Event.
        t.send(new HitBuilders.EventBuilder()
                .setCategory("Notifications")
                .setAction(type)
                .setLabel("Dergimi")
                .build());


        // initialize shared preference manager
        shared_pref = new SharedPreferenceNotification(this);

        if (shared_pref.getValue(type)) {
            Intent resultIntent;
            PendingIntent resultPendingIntent = null;




            if (type.equalsIgnoreCase("news"){

                resultIntent =new Intent(this, ViewInWeb.class);
                Bundle basket = new Bundle();
                basket.putString(TimeUtils.TAG_LINK,link );
                basket.putString(TimeUtils.TAG_TITLE,title );
                basket.putInt("TYPE", 1);
                resultIntent.putExtras(basket);

                t.send(new HitBuilders.EventBuilder()
                        .setCategory("Notifications")
                        .setAction(type)
                        .setLabel(title)
                        .build());

                resultPendingIntent = PendingIntent.getActivity(this, 0,
                        resultIntent, PendingIntent.FLAG_ONE_SHOT);

                displayNot(resultPendingIntent);

                Log.e("Erdhi ketu", "Erdhi ketuttttttt");
            }


        }
    }

    private void displayNot(PendingIntent resultPendingIntent){

        MyApplication mApplication = (MyApplication) getApplicationContext();

        mApplication.getTracker(MyApplication.TrackerName.APP_TRACKER);

        NotificationCompat.Builder mNotifyBuilder;
        NotificationManager mNotificationManager;

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotifyBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("AppName").setContentText(title)
                .setSmallIcon(R.drawable.ic_launcher);
        // Set pending intent
        mNotifyBuilder.setContentIntent(resultPendingIntent);

        // Set Vibrate, Sound and Light
        int defaults = 0;
        defaults = defaults | Notification.DEFAULT_LIGHTS;
        defaults = defaults | Notification.DEFAULT_VIBRATE;
        defaults = defaults | Notification.DEFAULT_SOUND;

        mNotifyBuilder.setDefaults(defaults);
        // Set the content for Notification
        mNotifyBuilder.setContentText(title);
        // Set autocancel
        mNotifyBuilder.setAutoCancel(true);
        // Post a notification
        mNotificationManager.notify(Integer.valueOf(nr),
                mNotifyBuilder.build());


    }

知道可能出了什么问题吗?

【问题讨论】:

  • 设置待定意图 PendingIntent.FLAG_UPDATE_CURRENT
  • 是的,可以,但是如果用户还没有打开通知,所以有两个通知,第一个取第二个的内容
  • 如果用户没有打开通知并且有 2 个通知来了,那么之前的通知就会更新,或者你可以说用这个覆盖..
  • 如果您只想在任何给定时间拥有一个Notification,您需要确保nr 始终具有相同的值。在这种情况下,当您发布新的Notification 时,它会覆盖旧的。

标签: android push-notification google-cloud-messaging android-pendingintent


【解决方案1】:

根据您的描述,您可能需要考虑使用GCM Notification Messages

通知消息由 Android 系统自动处理。您还可以设置 tag 字段,该字段将导致当前通知及其相关数据被新发送的通知和数据替换。

您还可以指定一个 click_action,它将确定用户在您的应用中点击通知的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    相关资源
    最近更新 更多