// 创建通知 private void createNotification() { // 创建通知管理者 manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 创建builder实例 builder = new Notification.Builder(MainActivity.this); // 设置小图标 builder.setSmallIcon(R.mipmap.ic_launcher); // builder.setContentTitle(""); // builder.setContentText(""); // 点击后不会自动消失 builder.setAutoCancel(false); // 不会滑动删除通知 builder.setOngoing(false); // 设置优先级 builder.setPriority(Notification.PRIORITY_MAX); // 通知提示语 builder.setTicker("notification is build!"); // 提示音 震动 闪光 都为系统默认 // builder.setDefaults(Notification.DEFAULT_ALL); builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.ring)); builder.setDefaults(Notification.DEFAULT_VIBRATE); // pendingIntent跳转 Intent intent = new Intent(MainActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); notification = builder.build(); // 发起正在运行事件(活动中) // notification.flags = Notification.FLAG_ONGOING_EVENT; // 三色灯提醒 // notification.flags = Notification.FLAG_SHOW_LIGHTS; // 让声音、振动无限循环,直到用户响应 (取消或者打开) notification.flags = Notification.FLAG_INSISTENT; // notification.flags = Notification.FLAG_ONLY_ALERT_ONCE ; // 只有全部清除时,Notification才会清除 // notification.flags = Notification.FLAG_NO_CLEAR; // 自定义通知样式 remoteView = new RemoteViews(getPackageName(), R.layout.layout_guard_notification); remoteView.setTextViewText(R.id.tv_notification_num, sdf.format(System.currentTimeMillis())); notification.contentView = remoteView; manager.notify(1990, notification); } 刷新通知:remoteView.setTextViewText(R.id.tv_notification_num, sdf.format(System.currentTimeMillis())); manager.notify(1990, notification);取消通知:二选一manager.cancel(1990);manager.cancelAll();
DEMO链接
通知:Notification
最新推荐文章于 2023-09-14 00:27:43 发布