Android 颤振:在锁定屏幕上显示通知

Android 颤振:在锁定屏幕上显示通知,android,flutter,Android,Flutter,首先,我看到过类似的问题。 但它只是提供我已经在使用的插件引用。 问题 我想在锁屏上显示通知。我已经尝试了可见性:NotificationVisibility.public,,但不确定为什么锁屏上没有显示通知 我正在物理设备上测试它。在我的设备上,其他应用程序正在发送通知,如whatsapp 这是我的密码 FlutterLocalNotificationsPlugin notificationsPlugin = FlutterLocalNotificationsPlugin(); //

首先,我看到过类似的问题。 但它只是提供我已经在使用的插件引用。 问题 我想在锁屏上显示通知。我已经尝试了
可见性:NotificationVisibility.public,
,但不确定为什么锁屏上没有显示通知

我正在物理设备上测试它。在我的设备上,其他应用程序正在发送通知,如whatsapp

这是我的密码

FlutterLocalNotificationsPlugin notificationsPlugin =
    FlutterLocalNotificationsPlugin();

//Function to handle Notification data in background.
Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) async {
  print("FCM backgroundMessageHandler $message");
  showNotification(DataNotification.fromPushMessage(message['data']));
  return Future<void>.value();
}

//Function to handle Notification Click.
Future<void> onSelectNotification(String payload) {
  print("FCM onSelectNotification");
  return Future<void>.value();
}

//Function to Parse and Show Notification when app is in foreground
Future<dynamic> onMessage(Map<String, dynamic> message) {
  print("FCM onMessage $message");
  print(message['data']['title']);
  showNotification(DataNotification.fromPushMessage(message['data']));

  return Future<void>.value();
}

//Function to Handle notification click if app is in background
Future<dynamic> onResume(Map<String, dynamic> message) {
  print("FCM onResume $message");
 return Future<void>.value();
}

//Function to Handle notification click if app is not in foreground neither in background
Future<dynamic> onLaunch(Map<String, dynamic> message) {
  print("FCM onLaunch $message");
  return Future<void>.value();
}

void showNotification(DataNotification notification) async {
  final AndroidNotificationDetails androidPlatformChannelSpecifics =
      await getAndroidNotificationDetails(notification);

  final NotificationDetails platformChannelSpecifics =
      NotificationDetails(android: androidPlatformChannelSpecifics);

  await notificationsPlugin.show(
    0,
    notification.title,
    notification.body,
    platformChannelSpecifics,
  );
}

Future<AndroidNotificationDetails> getAndroidNotificationDetails(
    DataNotification notification) async {
  switch (notification.notificationType) {
    case NotificationType.NEW_INVITATION:
    case NotificationType.NEW_MEMBERSHIP:
    case NotificationType.NEW_ADMIN_ROLE:
    case NotificationType.MEMBERSHIP_BLOCKED:
    case NotificationType.MEMBERSHIP_REMOVED:
    case NotificationType.NEW_MEMBERSHIP_REQUEST:
      return AndroidNotificationDetails(
        'organization',
        'Organization management',
        'Notifications regarding your organizations and memberships.',
        importance: Importance.max,
        priority: Priority.high,
        showWhen: false,
        playSound: true,
        ledColor: Colors.redAccent,
        color: Colors.amber,
        category: "Organization",
        icon: '@mipmap/ic_launcher',
        visibility: NotificationVisibility.public,
        largeIcon: DrawableResourceAndroidBitmap('@mipmap/ic_launcher'),
        styleInformation: await getBigPictureStyle(notification),
        //sound: RawResourceAndroidNotificationSound('slow_spring_board')
      );
    case NotificationType.NONE:
    default:
      return AndroidNotificationDetails(
        'general', 'General notifications',
        'General notifications that are not sorted to any specific topics.',
        importance: Importance.max,
        priority: Priority.high,
        showWhen: false,
        category: "General",
        playSound: true,
        ledColor: Colors.redAccent,
        color: Colors.amber,
        icon: '@mipmap/ic_launcher',
        visibility: NotificationVisibility.public,
        largeIcon: DrawableResourceAndroidBitmap('@mipmap/ic_launcher'),
        styleInformation: await getBigPictureStyle(notification),
        // sound: RawResourceAndroidNotificationSound('slow_spring_board')
      );
  }
}

Future<BigPictureStyleInformation> getBigPictureStyle(
    DataNotification notification) async {
  if (notification.imageUrl != null) {
    print("downloading");
    final String bigPicturePath =
        await _downloadAndSaveFile(notification.imageUrl, 'bigPicture');

    return BigPictureStyleInformation(FilePathAndroidBitmap(bigPicturePath),
        hideExpandedLargeIcon: true,
        contentTitle: notification.title,
        htmlFormatContentTitle: false,
        summaryText: notification.body,
        htmlFormatSummaryText: false);
  } else {
    print("NOT downloading");
    return null;
  }
}

Future<String> _downloadAndSaveFile(String url, String fileName) async {
  final Directory directory = await getApplicationDocumentsDirectory();
  final String filePath = '${directory.path}/$fileName';
  final http.Response response = await http.get(url);
  final File file = File(filePath);
  await file.writeAsBytes(response.bodyBytes);
  return filePath;
}

class NotificationService {
  FirebaseMessaging _fcm = FirebaseMessaging();

  void init() async {
    // Get.to(BookingQRScan());
    final AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');

    final IOSInitializationSettings initializationSettingsIOS =
        IOSInitializationSettings();

    final InitializationSettings initializationSettings =
        InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
    );

    await notificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (value) => onSelectNotification(value));

    _fcm.configure(
      onMessage: onMessage,
      onBackgroundMessage: backgroundMessageHandler,
      onLaunch: onLaunch,
      onResume: onResume,
    );
  }
}
flatterLocalNotificationsPlugin notificationsPlugin=
flatterLocalNotificationsPlugin();
//函数在后台处理通知数据。
Future backgroundMessageHandler(映射消息)异步{
打印(“FCM backgroundMessageHandler$message”);
showNotification(DataNotification.fromPushMessage(message['data']);
返回Future.value();
}
//用于处理通知单击的函数。
未来onSelectNotification(字符串负载){
打印(“FCM选择通知”);
返回Future.value();
}
//函数在应用程序位于前台时解析和显示通知
未来onMessage(地图消息){
打印(“FCM onMessage$message”);
打印(消息['data']['title']);
showNotification(DataNotification.fromPushMessage(message['data']);
返回Future.value();
}
//如果应用程序位于后台,则处理通知单击的函数
未来onResume(映射消息){
打印(“FCM onResume$信息”);
返回Future.value();
}
//如果应用程序不在前台也不在后台,则处理通知单击的函数
未来onLaunch(映射消息){
打印(“FCM onLaunch$消息”);
返回Future.value();
}
void showNotification(数据通知通知)异步{
最终AndroidNotificationDetails AndroidPlatformChannels规范=
等待getAndroidNotificationDetails(通知);
最终通知详情平台渠道详情=
通知详情(android:AndroidPlatformChannelSpecifications);
等待通知(
0,
通知.标题,
通知机构,
平台通道细节,
);
}
未来getAndroidNotificationDetails(
数据通知(DataNotification)异步{
开关(notification.notificationType){
案例通知类型。新邀请:
案例通知类型。新成员资格:
案例通知类型。新的管理员角色:
案例通知类型.MEMBERSHIP\u被阻止:
已删除案例通知类型.MEMBERSHIP\u:
案例通知类型。新成员资格申请:
返回AndroidNotificationDetails(
"组织",,
"组织管理",,
“关于您的组织和成员资格的通知。”,
重要性:重要性.max,
优先级:优先级,高,
showhen:false,
playSound:没错,
ledColor:Colors.redAccent,
颜色:颜色。琥珀色,
类别:“组织”,
图标:“@mipmap/ic_启动器”,
可见性:NotificationVisibility.public,
largeIcon:DrawableResourceAndroidBitmap(“@mipmap/ic_launcher”),
样式信息:等待getBigPictureStyle(通知),
//声音:RawResourceAndroidNotificationSound(“慢速弹簧板”)
);
案例通知类型。无:
违约:
返回AndroidNotificationDetails(
“一般”、“一般通知”,
“未按任何特定主题排序的常规通知。”,
重要性:重要性.max,
优先级:优先级,高,
showhen:false,
类别:“一般”,
playSound:没错,
ledColor:Colors.redAccent,
颜色:颜色。琥珀色,
图标:“@mipmap/ic_启动器”,
可见性:NotificationVisibility.public,
largeIcon:DrawableResourceAndroidBitmap(“@mipmap/ic_launcher”),
样式信息:等待getBigPictureStyle(通知),
//声音:RawResourceAndroidNotificationSound(“慢速弹簧板”)
);
}
}
未来getBigPictureStyle(
数据通知(DataNotification)异步{
if(notification.imageUrl!=null){
打印(“下载”);
最终字符串bigPicturePath=
等待下载并保存文件(notification.imageUrl,'bigPicture');
返回BigPictureStyleInformation(文件路径和IDBITMap(bigPicturePath)),
hideExpandedLargeIcon:true,
contentTitle:notification.title,
htmlFormatContentTitle:false,
summaryText:notification.body,
htmlFormatSummaryText:false);
}否则{
打印(“不下载”);
返回null;
}
}
Future\u downloadAndSaveFile(字符串url、字符串文件名)异步{
最终目录目录=等待getApplicationDocumentsDirectory();
最终字符串filePath='${directory.path}/$fileName';
final http.Response Response=wait http.get(url);
最终文件=文件(文件路径);
wait file.writeAsBytes(response.bodyBytes);
返回文件路径;
}
类通知服务{
FirebaseMessaging _fcm=FirebaseMessaging();
void init()异步{
//Get.to(BookingQRScan());
最终AndroidInitialization设置Initialization设置和ROID=
AndroidInitializationSettings(“@mipmap/ic_launcher”);
最终IOS初始化设置初始化设置SIOS=
IOSInitializationSettings();
最终初始化设置初始化设置=
初始化设置(
android:initializationSettingsAndroid,
iOS:初始化设置SIOS,
);
等待通知初始化(初始化设置,
onSelectNotification:(值)=>onSelectNotification(值));
_fcm.configure(
onMessage:onMessage,
onBackgroundMessage:backgroundMessageHandler,
onLaunch:onLaunch,
onResume:onResume,
);
}
}

我搜索了这个主题,发现我们无法以编程方式设置设置。 用户需要启用此设置

就像下面的截图。

我试图将用户重定向到“直接通知设置”页面,但未找到任何服务

因此,我挖掘了更多,找到了一个插件app\u settingsRaisedButton( onPressed: () { // AppSettings.openLocationSettings(); AppSettings.openNotificationSettings(); // openWIFISettings(); },