当应用程序处于后台时,Flutter Cloud Messaging 自定义通知声音不起作用
Posted
技术标签:
【中文标题】当应用程序处于后台时,Flutter Cloud Messaging 自定义通知声音不起作用【英文标题】:Flutter Cloud Messaging custom notification sound not working when the app is in background 【发布时间】:2021-10-22 05:17:05 【问题描述】:我的 Flutter 应用程序有一个自定义通知音,当应用程序处于前台时可以正常工作。但是当我关闭应用程序或将其保留在后台时,通知即将到来,但没有自定义通知声音。颤振版本 2.0,颤振本地通知:^5.0.0 firebase_messaging:^10.0.4。这是我的代码。任何人都可以帮我解决这个错误吗?谢谢你
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async
await Firebase.initializeApp();
print('Handling a background message $message.messageId');
androidNotificationChannel channel;
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
void main() async
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
if (!kIsWeb)
channel = const AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
'This channel is used for important notifications.',
importance: Importance.high,
enableLights: true,
sound: RawResourceAndroidNotificationSound('notification'),
playSound: true,
enableVibration: true,
);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
runApp(new MaterialApp(
home: MyApp(),
debugShowCheckedModeBanner: false,
routes: <String, WidgetBuilder>,
));
initializeFCM() async
FirebaseMessaging.onMessageOpenedApp.listen((message)
print('Message clicked!');
);
messaging = FirebaseMessaging.instance;
messaging.subscribeToTopic("messaging");
messaging.getToken().then((value) async
print("FirebaseToken:" + value);
var token = await getSharedPrefrence('token');
var notiToken = await sharedPrefrence('notiToken', value.toString());
if (token != null)
var sendToken = await sendNotiTokenApi(value.toString());
print(notiToken);
);
final sound = "notification.mp3";
FirebaseMessaging.onMessage.listen(
(RemoteMessage message)
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null && !kIsWeb)
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: 'launch_background',
importance: Importance.max,
priority: Priority.max,
enableLights: true,
enableVibration: true,
playSound: true,
sound: RawResourceAndroidNotificationSound('notification'),
),
));
showDialog(
context: context,
builder: (BuildContext context)
return AlertDialog(
title: Text("Notification"),
content: Text(message.notification.body),
actions: [
TextButton(
child: Text("Ok"),
onPressed: ()
Navigator.of(context).pop();
,
)
],
);
);
,
);
void showNotification(message) async
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
Platform.isAndroid
? 'com.dfa.flutterchatdemo'
: 'com.duytq.flutterchatdemo',
'Flutter chat demo',
'your channel description',
playSound: true,
enableVibration: true,
sound: RawResourceAndroidNotificationSound('notification'),
importance: Importance.max,
enableLights: true,
priority: Priority.high,
);
var iosPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics);
print(
message['notification']['title'].toString(),
);
await flutterLocalNotificationsPlugin.show(
0,
message['notification']['title'].toString(),
message['notification']['body'].toString(),
platformChannelSpecifics,
payload: 'test'
// payload: json.encode(message)
);
void configLocalNotification()
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
iOS: initializationSettingsIOS, android: initializationSettingsAndroid);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
【问题讨论】:
【参考方案1】:对于 onMessage,您使用 flutterLocalNotificationsPlugin.show 手动推送自定义通知,但对于 _firebaseMessagingBackgroundHandler,使用默认通知推送器。 p>
修复: 您可以尝试初始化所需的对象并像当前的 flutterLocalNotificationsPlugin.show
一样对其进行自定义【讨论】:
当我复制 flutterLocaNotificationsPlugin.show 时,我收到以下错误消息:FlutterFire 消息:后台消息处理程序中发生错误:NoSuchMethodError:getter 'id' 被调用为 null。以上是关于当应用程序处于后台时,Flutter Cloud Messaging 自定义通知声音不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:当应用程序处于后台但未在 iOS 中终止时,无法在通知单击时重定向到特定屏幕
Flutter Firebase Cloud Messaging - 应用程序在后台时的通知
Flutter - 如何在应用程序处于后台时在特定时间执行功能?