Android顯示不重復通知的Notification


在Android開發中經常會用到Notification來展示通知,但是之前寫出來的代碼中一個APP只能有一個通知,有新的通知的時候自己會覆蓋之前的通知,一直不知道為什么,好,話不多說,先貼我之前的代碼

    private void showNotification(String title, Context context) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,
                "XXX", System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.vibrate = new long[]{0, 100, 200, 300};
        Intent intent = null;
        if (pushType == 1) {
            intent = new Intent(context, Advertisement.class);
        } else if (pushType == 2) {
            intent = new Intent(context, HomePage.class);
        } else if (pushType == 3) {
            intent = new Intent(context, OrderList.class);
        }

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                intent, 0);
        notification.setLatestEventInfo(context, "XXX", title, contentIntent);
        notificationManager.notify(111, notification);
    }

  這幾天,看了Android關於Notification的源碼,發現了notify函數的一段說明

 

/**
     * Post a notification to be shown in the status bar. If a notification with
     * the same id has already been posted by your application and has not yet been canceled, it
     * will be replaced by the updated information.
     *
     * @param id An identifier for this notification unique within your
     *        application.
     * @param notification A {@link Notification} object describing what to show the user. Must not
     *        be null.
     */
    public void notify(int id, Notification notification)
    {
        notify(null, id, notification);
    }

  

在這里可以看到,Notification中的第一個參數id就是每一個通知的id,按照常理來推斷,只要id一樣,自然就會覆蓋,既然這樣,那就用時間戳來代替上面的寫法好啦,新代碼如下:

 

  private void showNotification(String title, Context context) {
        int requestCode = (int) System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,
                "90上門洗車", System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.vibrate = new long[]{0, 100, 200, 300};
        Intent intent = null;
        if (pushType == 1) {
            intent = new Intent(context, Advertisement.class);
        } else if (pushType == 2) {
            intent = new Intent(context, HomePage.class);
        } else if (pushType == 3) {
            intent = new Intent(context, OrderList.class);
        }

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                intent, 0);
        notification.setLatestEventInfo(context, "90上門洗車", title, contentIntent);
        notificationManager.notify(requestCode, notification);
    }

  

在這里,我把notify()里面的id參數變成了時間戳,編譯,運行,好像成功了呢,這樣,就可以顯示不同的通知啦~

 

 =  =  雖然成功了,但是在源碼里面可以看到,還有一種重載

 

 

 /**
     * Post a notification to be shown in the status bar. If a notification with
     * the same tag and id has already been posted by your application and has not yet been
     * canceled, it will be replaced by the updated information.
     *
     * @param tag A string identifier for this notification.  May be {@code null}.
     * @param id An identifier for this notification.  The pair (tag, id) must be unique
     *        within your application.
     * @param notification A {@link Notification} object describing what to
     *        show the user. Must not be null.
     */
    public void notify(String tag, int id, Notification notification)
    {
        int[] idOut = new int[1];
        INotificationManager service = getService();
        String pkg = mContext.getPackageName();
        if (notification.sound != null) {
            notification.sound = notification.sound.getCanonicalUri();
            if (StrictMode.vmFileUriExposureEnabled()) {
                notification.sound.checkFileUriExposed("Notification.sound");
            }
        }
        if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
        Notification stripped = notification.clone();
        Builder.stripForDelivery(stripped);
        try {
            service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
                    stripped, idOut, UserHandle.myUserId());
            if (id != idOut[0]) {
                Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
            }
        } catch (RemoteException e) {
        }
    }

  

這個,才是之前的notifiy()方法調用的真實的函數,不過就是tag在之前的調用中被設置成了null,在這段代碼的注釋中說道tag和id都是可以拿來區分不同的通知的,我們可以把這兩個混起來使用,實現類似於qq一樣的效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM