【发布时间】:2021-03-26 04:44:57
【问题描述】:
首先,我在 SO 上看到了类似的问题。 Show notifications in lock screen by flutter
但它只提供我已经在使用的插件引用。
问题
我想在锁定屏幕上显示通知。我已经尝试过visibility: 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,
);
}
}
【问题讨论】: