问题描述
我正在使用 Flutterlocalnotifications 包和 FCM 向我的用户发送通知。一切正常。当我的应用程序在后台、前台或关闭时,我可以向我的用户显示通知。我可以在前台和后台达到有效载荷值,但是当应用程序被杀死时,我无法再达到有效载荷。我在 catlog 中测试,但 onSelectNotification 中没有任何打印。如何达到这个有效载荷?这是我的通知处理程序类
import 'package:Flutter/cupertino.dart';
import 'package:Flutter/material.dart';
import 'package:Flutter_local_notifications/Flutter_local_notifications.dart';
AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel',// id
'High Importance Notifications',// title
'This channel is used for important notifications.',// description
importance: Importance.high,);
final FlutterlocalnotificationsPlugin FlutterlocalnotificationsPlugin =
FlutterlocalnotificationsPlugin();
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// await Firebase.initializeApp();
//Bildirim geldiği anda datayı elde ettiğimiz alan
final dynamic data = message.data;
print('Data in background : ${data.toString()}');
NotificationHandler.showNotification(data);
}
class NotificationHandler {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
static final NotificationHandler _singleton = NotificationHandler._internal();
factory NotificationHandler() {
return _singleton;
}
NotificationHandler._internal();
BuildContext myContext;
initializefCMNotifications(BuildContext context) async {
await _firebaseMessaging.setForegroundNotificationPresentationoptions(
alert: true,badge: true,sound: true);
var initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = IOSInitializationSettings(
onDidReceivelocalnotification: onDidReceivelocalnotification);
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,iOS: initializationSettingsIOS);
await FlutterlocalnotificationsPlugin.initialize(initializationSettings,onSelectNotification: onSelectNotification);
await _firebaseMessaging.subscribetoTopic("all");
await FirebaseMessaging.instance
.getinitialMessage()
.then((RemoteMessage message) {
if (message != null) {
print('initialmessage tetiklendi : ' + message.data.toString());
}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
var notification = message.notification;
var android = message.notification?.android;
print('ONMESSAGE ÇALIŞTI');
print('onmessage tetiklendi data : ' +
message.data['message'] +
' title ' +
message.data['title']);
showNotification(message.data);
});
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('Onmessageopened : ' + message.data.toString());
});
}
static void showNotification(Map<String,dynamic> data) async {
const androidplatformChannelSpecifics = AndroidNotificationDetails(
'1234','Yeni Mesaj','your channel description',importance: Importance.max,priority: Priority.high,ticker: 'ticker');
const IOSPlatformChannelSpecifics = IOSNotificationDetails();
const platformChannelSpecifics = NotificationDetails(
android: androidplatformChannelSpecifics,iOS: IOSPlatformChannelSpecifics);
await FlutterlocalnotificationsPlugin.show(
0,data['title'],data['message'],platformChannelSpecifics,payload: 'The value when notification tapped');
}
Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload : $payload');
}
}
Future onDidReceivelocalnotification(
int id,String title,String body,String payload) {}
}
这些是我使用的软件包的版本
firebase_messaging: ^9.1.1
Flutter_local_notifications: ^5.0.0+1
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)