Flutter iOS端推送通知无法正常工作的技术求助
Flutter iOS端推送通知无法显示(Android端正常)
我在Flutter项目中遇到iOS端推送通知无法显示的问题,但Android端运行完全正常。相关情况如下:
- 使用
flutter_local_notification包实现通知 - 采用物理iOS设备测试
- 已在
appdelegate.swift中添加必要配置代码
云函数代码
// Fetch the first name of the user who created the appointment const creatorSnapshot = await admin .firestore() .collection("users") .doc(appointment.createdBy) .get(); const creator = creatorSnapshot.data(); const creatorName = creator ? creator.first_name : "Someone"; const requestedUserId = appointment.createdBy; // Get all users to send notification to, excluding the creator const usersSnapshot = await admin.firestore().collection("users").get(); const tokens = []; usersSnapshot.forEach((doc) => { if (doc.id !== appointment.createdBy) { const userToken = doc.data().fcmToken; if (userToken) { tokens.push(userToken); } } }); if (tokens.length === 0) { console.log("No valid tokens found. Notification not sent."); return null; } // Constructing the FCM message const message = { notification: { title: "New Session Available", body: `${creatorName} has created an appointment for ${appointment.workouts.join( ", " )} at ${appointment.date_time .toDate() .toLocaleString()}, are you available?`, }, data: { type: "new_appointment", appointmentId: appointmentId, requestedUserId: requestedUserId, }, tokens: tokens, }; // Sending the FCM message return admin .messaging() .sendMulticast(message) .then((response) => { console.log(response.successCount + " messages were sent successfully"); });
Flutter端通知处理代码
// iOS Initialization var initializationSettingsIOS = DarwinInitializationSettings( requestAlertPermission: true, requestBadgePermission: true, requestSoundPermission: true, onDidReceiveLocalNotification: onDidReceiveLocalNotification, ); // request permissions //for ios if (Platform.isIOS) { await flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation< IOSFlutterLocalNotificationsPlugin>() ?.requestPermissions( alert: true, badge: true, sound: true, ); //for android } else if (Platform.isAndroid) { final AndroidFlutterLocalNotificationsPlugin? androidImplementation = flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>(); // ignore: unused_local_variable final bool? grantedNotificationPermission = await androidImplementation?.requestNotificationsPermission(); } // InitializationSettings for both platforms var initializationSettings = InitializationSettings( android: initializationSettingsAndroid, iOS: initializationSettingsIOS); saveUserTokenToFirestore(); print('step 1'); // initialize the plugins await flutterLocalNotificationsPlugin.initialize( initializationSettings, onDidReceiveNotificationResponse: (NotificationResponse notificationResponse) { if (notificationResponse.notificationResponseType == NotificationResponseType.selectedNotification) { selectNotificationSubject.add(notificationResponse.payload); } }, ); onNotificationTap(); FirebaseMessaging.onMessage.listen((RemoteMessage message) { print('step 2'); showNotification(message); }); FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { onNotificationTap(); }); // Android Notification Details var androidDetails = const AndroidNotificationDetails( 'new_appointment', 'Appointment Notifications', channelDescription: 'Get notified about new appointments and updates', importance: Importance.max, ); // iOS Notification Details var iosDetails = const DarwinNotificationDetails( presentAlert: true, presentSound: true, subtitle: 'Notification'); var notificationDetails = NotificationDetails(android: androidDetails, iOS: iosDetails); String payload = json.encode(message.data); // Display the notification using the flutterLocalNotificationsPlugin await flutterLocalNotificationsPlugin.show( message.hashCode, message.notification?.title ?? 'Default Title', message.notification?.body ?? 'Default body', notificationDetails, payload: payload, );
Info.plist配置
<key>FirebaseAppDelegateProxyEnabled</key> <false/> <key>UIBackgroundModes</key> <array> <string>fetch</string> <string>remote-notification</string> </array>
我已经尝试了网上所有相关教程,但iOS端推送通知仍无法正常工作,恳请协助解决。
内容的提问来源于stack exchange,提问作者AKEREDOLU OLUGBENGA
相关产品推荐
相关产品推荐

