我的问题是 “通知出现在通知栏中,但通知横幅弹出窗口不会出现在后台或终止状态。横幅在前台状态收到通知后可以正常工作。可能是什么问题?”
下面粘贴main.dart文件的代码
Future<void> main() async {
await runZonedGuarded(
() async {
WidgetsFlutterBinding.ensureInitialized();
Bloc.observer = AppBlocObserver();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
FlutterError.onError = (details) {
log(details.exceptionAsString(), stackTrace: details.stack);
};
///firebase initialize
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebasePerformance performance = FirebasePerformance.instance;
performance.isPerformanceCollectionEnabled();
NotificationServices.getFirebaseMessagingToken().then((_) {
NotificationServices().firebaseInit();
});
FlutterError.onError =
FirebaseCrashlytics.instance.recordFlutterFatalError;
PlatformDispatcher.instance.onError = (error, stack) {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
return true;
};
runApp(child: const MyApp())));
},
(error, stackTrace) => log(error.toString(), stackTrace: stackTrace),
);
}
这里我粘贴Notification服务类的代码
class NotificationServices {
static String? firebasePushToken;
static FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;
static final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
static void initLocalNotifications(RemoteMessage message) async {
var androidInitializationSettings =
const AndroidInitializationSettings('@mipmap/launcher_icon');
var iosInitializationSettings = const DarwinInitializationSettings();
var initializationSetting = InitializationSettings(
android: androidInitializationSettings, iOS: iosInitializationSettings);
await _flutterLocalNotificationsPlugin.initialize(
initializationSetting,
onDidReceiveNotificationResponse: (payload) {
handleMessage(message);
},
);
}
Future<void> firebaseInit() async {
FirebaseMessaging.onMessage.listen((message) async {
String? receiverId = await PrefManager.retrieveValue(
Constants.id);
String senderId = message.data['senderId'];
if(receiverId != null && senderId != receiverId)
{
if (Platform.isIOS) {
initLocalNotifications(message);
} else {
initLocalNotifications(message);
showNotification(message);
}
}else{
print("receiver id $receiverId senderId ${message.data['senderId']}");
}
});
FirebaseMessaging.onMessageOpenedApp.listen((message) {
print("on Message Opened");
handleMessage(message);
// Use NavigationService.navigateTo or other navigation logic here
}
);
}
static Future<void> showNotification(RemoteMessage message) async {
print('inside showNotification');
const AndroidNotificationChannel androidNotificationChannel =
AndroidNotificationChannel(
"high_importance_channel", "High Importance Notifications",
description: 'This channel is used for important notifications.',
importance: Importance.max);
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
androidNotificationChannel.id, androidNotificationChannel.name,
channelDescription:
'This channel is used for important notifications.',
importance: Importance.high,
priority: Priority.high);
const DarwinNotificationDetails darwinNotificationDetails =
DarwinNotificationDetails(
presentAlert: true, presentBadge: true, presentSound: true);
NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails,
iOS: darwinNotificationDetails,
);
Future.delayed(Duration.zero, () {
_flutterLocalNotificationsPlugin.show(
0,
message.notification?.title.toString(),
message.notification?.body.toString(),
notificationDetails);
});
}
static Future<String?> getFirebaseMessagingToken() async {
NotificationSettings settings = await firebaseMessaging.requestPermission(
alert: true,
announcement: true,
badge: true,
carPlay: true,
criticalAlert: false,
provisional: false,
sound: true,
);
log('User granted firebase permission: ${settings.authorizationStatus}');
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
await firebaseMessaging.getToken().then((token) {
if (token != null) {
firebasePushToken = token;
log("fcm token : $token");
}
});
await PrefManager.storeValue(
Constants.firebaseDeviceToken, firebasePushToken);
return firebasePushToken;
}
///Handling incoming message
static Future<void> handleMessage(RemoteMessage? message) async {
if (message == null) {
return;
} else {
String isGroupChat = message.data["isGroupChat"];
String senderId = message.data["senderId"];
String? receiverId = await PrefManager.retrieveValue(
Constants.id);
if (receiverId != null && message.notification?.title != null) {
if(isGroupChat == 'true')
{
print("inside groupChat ");
NavigationService.navigateTo(AppRoutes.groupChatScreen,arguments: {
'currentUserId':receiverId,
'topicName':message.data["topicName"],
'groupName':message.notification?.title,
});
}else{
NavigationService.navigateTo(AppRoutes.chatScreen, arguments: {
'name': senderName,
'userId': senderId,
'currentUserId': receiverId,
});
}
} else {
print(" null");
}
print("notification msg");
}
}
}
通知出现在通知栏中,但通知横幅弹出窗口不会出现在后台或终止状态。在前台状态收到通知后,横幅可以正常工作。可能是什么问题?.
有效负载详细信息
final body = {
"to":groupChat? "/topics/$pushToken": pushToken,
"notification": {
"android_channel_id": "high_importance_channel",
"title":groupChat? groupName:senderName,
"body":groupChat?'$senderName : $msg':msg,
"priority": "high",
"time_to_live": 86400,
"click_action": "FLUTTER_NOTIFICATION_CLICK",
},
"data":{
"chatId":chatId,
"senderId":senderId,
"isGroupChat":groupChat,
"topicName":pushToken,
"memberCount":memberCount,
"groupId":groupId
},
"android": {
"priority": "high"
},
"apns": {
"payload": {
"aps": {
"contentAvailable": true
}
}
}
};
此问题是区域设置通知还是远程通知发生的?
如果您使用服务器发送通知,您可以分享有效负载的详细信息吗?
您可以在移动应用程序中更新的内容是将其添加到您的
AndroidManifest.xml
...
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
</application>
</manifest>
并且您还可以将重要性和优先级设置为“max”
AndroidNotificationDetails(
'high_importance_channel',
'YOUR_CHANNEL_NAME',
channelDescription: 'YOUR_CHANNEL_DESCRIPTION',
importance: Importance.max,
priority: Priority.max,
)
您还可以将服务器负载中的重要性设置为
"max"
。