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

 

posted @ 2015-04-13 14:25  小菜小白  阅读(3983)  评论(0编辑  收藏  举报