Android顯示不週期性通知的Notification

來源:互聯網
上載者:User

標籤:

在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一樣的效果

 

Android顯示不週期性通知的Notification

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.