18/3/13$
public void sendNotification() { Intent resultIntent = new Intent(this, MainActivity.class); PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher_background) .setContentTitle("Notification") .setContentText("test") .setPriority(Notification.PRIORITY_MAX) //.setOngoing(true) //.setDefaults(Notification.DEFAULT_VIBRATE) .setVisibility(Notification.VISIBILITY_PUBLIC) .setContentIntent(resultPendingIntent); //.setWhen(System.currentTimeMillis() + System.currentTimeMillis()); Notification notification = builder.build(); notification.flags |= Notification.FLAG_NO_CLEAR; //notification.flags |= Notification.FLAG_ONGOING_EVENT; notification.flags |= Notification.FLAG_AUTO_CANCEL; // Runnable runnable = new Runnable() { // @Override // public void run() { // mnotifyManager.notify(1, notification); // } // }; // handler.postDelayed(runnable,2000); // if (Build.VERSION.SDK_INT >= 26) { // intent = new Intent(MainActivity.this, SecondActivity.class); // PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0); String id = "channel_1"; String description = "渠道1"; int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel mChannel = new NotificationChannel(id, description, importance); mChannel.setDescription(description); mChannel.enableLights(true); mChannel.setLightColor(Color.RED); mChannel.setImportance(IMPORTANCE_MAX); //mChannel.enableVibration(true); // // mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); notificationManager = mnotifyManager; mNotificationManager = notificationManager; mNotificationManager.createNotificationChannel(mChannel); notification = new Notification.Builder(MainActivity.this, id).setContentTitle("Title") .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentTitle("您有一条新通知") .setContentText("这是一条xx的消息") .setAutoCancel(true) // .setContentIntent(pintent) .build(); mNotificationManager.notify(1, notification); // }else{ // sendNotification_24(); // } }
8.1新通知构成用例
渠道构造体
通知构造体