PHP
·
发表于 5年以前
·
阅读量:8410
关键点在这个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);
}