发送不重复的通知Notification


关键点在这个requestCode,这里使用的是当前系统时间,巧妙的保证了每次都是一个新的Notification产生

public static void sendNotification(Context context, String title,String message, Bundle extras) {
    Intent mIntent = new Intent(context, FragmentTabsActivity.class);
    mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mIntent.putExtras(extras);

    int requestCode = (int) System.currentTimeMillis();

    PendingIntent mContentIntent = PendingIntent.getActivity(context,
            requestCode, mIntent, 0);

    Notification mNotification = new NotificationCompat.Builder(context)
            .setContentTitle(title).setSmallIcon(R.drawable.app_icon)
            .setContentIntent(mContentIntent).setContentText(message)
            .build();
    mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
    mNotification.defaults = Notification.DEFAULT_ALL;

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

    mNotificationManager.notify(requestCode, mNotification);
}
相关标签

扫一扫

在手机上阅读