Flutter 本地通知声音适用于除 Samsung A50 以外的大多数设备 Android 11
Flutter local notification sound works in most devices except Samsung A50 Android 11
我想在用户打开我的 flutter 应用程序时显示推送通知。通知出现在大多数设备中并且有声音并且在三星和 Realme 中看起来像弹出窗口但在我的设备三星 A50 中,通知出现但没有声音并且不弹出,只出现在通知栏中。我在 Android 项目的 res 文件夹中创建了一个原始文件夹。
这是我的代码
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:rxdart/rxdart.dart';
class NotificationApi{
static final notifications = FlutterLocalNotificationsPlugin();
static final onNotifications = BehaviorSubject<String?>();
static Future notificationsDetails() async {
var androidPlatformChannelSpecifics =
AndroidNotificationDetails(
'Notification Channel ID',
'Channel Name',
importance: Importance.max,
priority: Priority.high,
sound: RawResourceAndroidNotificationSound('sound'),
// ongoing: true,
);
var iOSPlatformChannelSpecifics = IOSNotificationDetails(
// sound: 'slow_spring_board.aiff'
);
return
NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics
);
}
static Future init({bool initScheduled = false}) async {
final android = AndroidInitializationSettings('@mipmap/ic_launcher');
final ios = IOSInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
);
final settings = InitializationSettings(android: android,iOS: ios);
await notifications.initialize(
settings,
onSelectNotification: (payload) async {
onNotifications.add(payload);
});
}
static Future showNotification({int? id,
String? title,
String? body,
String? payload}) async {
notifications.show(id!, title, body, await notificationsDetails(),payload: payload);
}
}
在我的home.dart
@override
void initState() {
super.initState();
NotificationApi.init();
listenNotifications();
/// some code
showLocalNotification();
}
listenNotifications(){
NotificationApi.onNotifications.stream.listen((event) {
Navigator.of(context).push(MaterialPageRoute(builder: (context)=>InboxClass()));
});
}
showLocalNotification(List<OfferModel> offerList){
for(var i=0;i<offerList.length;i++){
NotificationApi.showNotification(title: offerList[i].NAME,body:offerList[i].DESCRIPTION,payload: "",id: i);
}
}
可能是重要性参数有误。
importance: Importance.max
应该是
importance: Importance.high
Importance.max 未被 Android 使用,参见 here
我想在用户打开我的 flutter 应用程序时显示推送通知。通知出现在大多数设备中并且有声音并且在三星和 Realme 中看起来像弹出窗口但在我的设备三星 A50 中,通知出现但没有声音并且不弹出,只出现在通知栏中。我在 Android 项目的 res 文件夹中创建了一个原始文件夹。
这是我的代码
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:rxdart/rxdart.dart';
class NotificationApi{
static final notifications = FlutterLocalNotificationsPlugin();
static final onNotifications = BehaviorSubject<String?>();
static Future notificationsDetails() async {
var androidPlatformChannelSpecifics =
AndroidNotificationDetails(
'Notification Channel ID',
'Channel Name',
importance: Importance.max,
priority: Priority.high,
sound: RawResourceAndroidNotificationSound('sound'),
// ongoing: true,
);
var iOSPlatformChannelSpecifics = IOSNotificationDetails(
// sound: 'slow_spring_board.aiff'
);
return
NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics
);
}
static Future init({bool initScheduled = false}) async {
final android = AndroidInitializationSettings('@mipmap/ic_launcher');
final ios = IOSInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
);
final settings = InitializationSettings(android: android,iOS: ios);
await notifications.initialize(
settings,
onSelectNotification: (payload) async {
onNotifications.add(payload);
});
}
static Future showNotification({int? id,
String? title,
String? body,
String? payload}) async {
notifications.show(id!, title, body, await notificationsDetails(),payload: payload);
}
}
在我的home.dart
@override
void initState() {
super.initState();
NotificationApi.init();
listenNotifications();
/// some code
showLocalNotification();
}
listenNotifications(){
NotificationApi.onNotifications.stream.listen((event) {
Navigator.of(context).push(MaterialPageRoute(builder: (context)=>InboxClass()));
});
}
showLocalNotification(List<OfferModel> offerList){
for(var i=0;i<offerList.length;i++){
NotificationApi.showNotification(title: offerList[i].NAME,body:offerList[i].DESCRIPTION,payload: "",id: i);
}
}
可能是重要性参数有误。
importance: Importance.max
应该是
importance: Importance.high
Importance.max 未被 Android 使用,参见 here