当前位置:   article > 正文

Flutter 通知栏通知_flutter通知栏提醒

flutter通知栏提醒

 本章节叙述Flutter 通知栏通知功能,主要是使用 Flutter与原生交互功能调用Android发送通知。

效果图

所需知识

Flutter构建通道机制Channel
Android创建通知渠道NotificationChannel发送通知

实现代码

 Flutter端主要实现与原生交互并传递数据

  1. //获取到插件与原生的交互通道
  2. static const mNotificationBar = const MethodChannel('notification_bar.flutter.io/notificationBar');
  3. //发动数据到原生 并返回
  4. String result = await mNotificationBar.invokeMethod('content', map);

 Android端完成两个动作:1构建原生交互通道与flutter对应。2实现发送通知功能

  1. //构建原生通道
  2. new MethodChannel(getFlutterView(), "notification_bar.flutter.io/notificationBar").setMethodCallHandler(
  3. new MethodCallHandler() {
  4. @Override
  5. public void onMethodCall(MethodCall call, Result result) {
  6. // TODO
  7. if (call.method.equals("content")) {
  8. //解析参数
  9. String contentTitle = call.argument("contentTitle");
  10. String contentText = call.argument("contentText");
  11. // 发送通知
  12. sendChatMsg(contentTitle,contentText);
  13. if (true) {
  14. result.success("success");
  15. } else {
  16. result.error("error", "failure", null);
  17. }
  18. } else {
  19. result.notImplemented();
  20. }
  21. }
  22. }
  23. );
  1. // 创建两个通知渠道 8.0新特性,不允许使用 NotificationCompat.Builder
  2. private void initNotificationManager(){
  3. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  4. String channelId = "chat";
  5. String channelName = "聊天消息";
  6. int importance = NotificationManager.IMPORTANCE_HIGH;
  7. createNotificationChannel(channelId, channelName, importance);
  8. }
  9. }
  10. @TargetApi(Build.VERSION_CODES.O)
  11. private void createNotificationChannel(String channelId, String channelName, int importance) {
  12. NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
  13. channel.setShowBadge(true);//开启桌面角标
  14. channel.setBypassDnd(true); //设置绕过免打扰模式
  15. channel.canBypassDnd(); //检测是否绕过免打扰模式
  16. channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//设置在锁屏界面上显示这条通知
  17. //是否需要振动提示
  18. channel.enableVibration(true);
  19. //振动模式
  20. channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
  21. //是否需要呼吸灯提示
  22. channel.enableLights(true);
  23. //呼吸灯颜色
  24. // channel.setLightColor();
  25. NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE);
  26. notificationManager.createNotificationChannel(channel);
  27. }
  28. public void sendChatMsg(String contentTitle,String contentText) {
  29. NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  30. //检查权限是否开启
  31. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  32. NotificationChannel channel = manager.getNotificationChannel("chat");
  33. if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
  34. Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
  35. intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
  36. intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
  37. startActivity(intent);
  38. Toast.makeText(this, "请手动将通知打开", Toast.LENGTH_SHORT).show();
  39. }
  40. }
  41. Notification notification = new NotificationCompat.Builder(this, "chat")
  42. .setContentTitle(contentTitle)
  43. .setContentText(contentText)
  44. .setWhen(System.currentTimeMillis())
  45. .setSmallIcon(R.mipmap.ic_launcher)
  46. .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
  47. .setAutoCancel(true)
  48. .setNumber(19)//角标显示数字
  49. .build();
  50. manager.notify(1, notification);
  51. }

完整代码

查看完整代码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/493966
推荐阅读
相关标签
  

闽ICP备14008679号