This section describes the Flutter notification function, mainly using the Flutter and native interaction function to call Android to send notifications.
Design sketch
Required knowledge
Flutter builds channel mechanism Channel
Android creates notification channel Notification Channel to send notifications
Implementation code
Flutter mainly implements interaction with native and transfers data
//Get the interaction channel between the plug-in and the native static const mNotificationBar = const MethodChannel('notification_bar.flutter.io/notificationBar'); //Launch data to native and return String result = await mNotificationBar.invokeMethod('content', map);
The Android end completes two actions: 1. Constructing native interaction channels corresponds to flutter. 2. Realize the function of sending notification
//Constructing Primary Channel new MethodChannel(getFlutterView(), "notification_bar.flutter.io/notificationBar").setMethodCallHandler( new MethodCallHandler() { @Override public void onMethodCall(MethodCall call, Result result) { // TODO if (call.method.equals("content")) { //Analytic parameter String contentTitle = call.argument("contentTitle"); String contentText = call.argument("contentText"); // Sending notice sendChatMsg(contentTitle,contentText); if (true) { result.success("success"); } else { result.error("error", "failure", null); } } else { result.notImplemented(); } } } );
// Create two new notification channels 8.0 features that do not allow NotificationCompat.Builder private void initNotificationManager(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "chat"; String channelName = "Chat messages"; int importance = NotificationManager.IMPORTANCE_HIGH; createNotificationChannel(channelId, channelName, importance); } } @TargetApi(Build.VERSION_CODES.O) private void createNotificationChannel(String channelId, String channelName, int importance) { NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); channel.setShowBadge(true);//Open Desktop Corner channel.setBypassDnd(true); //Set bypass interference-free mode channel.canBypassDnd(); //Detecting whether to bypass interference-free mode channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//Set up the lock screen interface to display this notification //Whether Vibration Tips are Needed channel.enableVibration(true); //Vibration mode channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); //Do you need a breathing light? channel.enableLights(true); //Breathing lamp color // channel.setLightColor(); NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); } public void sendChatMsg(String contentTitle,String contentText) { NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //Check whether permissions are open if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = manager.getNotificationChannel("chat"); if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()); intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId()); startActivity(intent); Toast.makeText(this, "Please open the notification manually", Toast.LENGTH_SHORT).show(); } } Notification notification = new NotificationCompat.Builder(this, "chat") .setContentTitle(contentTitle) .setContentText(contentText) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setAutoCancel(true) .setNumber(19)//Corner Display Number .build(); manager.notify(1, notification); }