Preface:
我的应用程序基于 Flutter - 但需要本机代码实现才能使 FCM 消息正常工作,请参阅下文了解更多详细信息
GitHub问题 #154 https://github.com/ConnectyCube/connectycube-flutter-samples/issues/154以供参考。
我在 iOS 上获取 FCM 通知时遇到了巨大的困难,特别是在我发布到 Testflight 的应用程序上。我已经被这个问题困扰了一个星期,完全不知道如何继续。
Problem
当使用 Xcode/Android Studio 在我的设备上使用调试/发布版本本地运行时,会在后台、前台等收到通知。exact与 Testflight 相同的应用程序,不会通过 FCM 发出任何通知。
这一点至关重要,因为 FCM 提供 VoIP 通知,而 Testflight 上却收不到这些通知,这非常令人痛苦
问题与解决方案?
我发现了 2 个问题(here https://stackoverflow.com/questions/39550615/firebase-notification-not-working-on-testflight & here https://stackoverflow.com/questions/41140631/firebase-push-notifications-not-working-on-testflight-adhoc-release),两者似乎都表明这是 APNS 证书问题(APNS -> Firebase)。我已经重新创建了我的证书并将其添加到 Firebase 控制台(使用相同的.csr
所有证书生成操作的文件)
设置/配置:
-
APNS生成密钥并添加到 Firebase
-
能力:
尝试过:
<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>
with:
<key>FirebaseAppDelegateProxyEnabled</key>
<string>0</string>
与 :
<key>FirebaseAppDelegateProxyEnabled</key>
<boolean>false</boolean>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>bluetooth-central</string>
<string>external-accessory</string>
<string>fetch</string>
<string>location</string>
<string>processing</string>
<string>remote-notification</string>
<string>voip</string>
<string>remote-notification</string>
</array>
教程/来源:
- 设置 FCM 和 APNS https://firebase.flutter.dev/docs/messaging/apple-integration/
- ConnectyCube P2P 会话源 https://github.com/ConnectyCube/connectycube-flutter-samples/tree/master/p2p_call_sample
SWIFT代码:(目标 >=10.0)
import UIKit
import CallKit
import Flutter
import Firebase
import UserNotifications
import GoogleMaps
import PushKit
import flutter_voip_push_notification
import flutter_call_kit
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, PKPushRegistryDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// run firebase app
FirebaseApp.configure()
// setup Google Maps
GMSServices.provideAPIKey("google-maps-api-key")
// register notification delegate
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
GeneratedPluginRegistrant.register(with: self)
// register VOIP
self.voipRegistration()
// register notifications
application.registerForRemoteNotifications();
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
// Handle updated push credentials
public func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
// Process the received pushCredentials
FlutterVoipPushNotificationPlugin.didUpdate(pushCredentials, forType: type.rawValue);
}
// Handle incoming pushes
public func pushRegistry(_ registry: PKPushRegistry,
didReceiveIncomingPushWith payload: PKPushPayload,
for type: PKPushType,
completion: @escaping () -> Swift.Void){
FlutterVoipPushNotificationPlugin.didReceiveIncomingPush(with: payload, forType: type.rawValue)
let signalType = payload.dictionaryPayload["signal_type"] as! String
if(signalType == "endCall" || signalType == "rejectCall"){
return
}
let uuid = payload.dictionaryPayload["session_id"] as! String
let uID = payload.dictionaryPayload["caller_id"] as! Int
let callerName = payload.dictionaryPayload["caller_name"] as! String
let isVideo = payload.dictionaryPayload["call_type"] as! Int == 1;
FlutterCallKitPlugin.reportNewIncomingCall(
uuid,
handle: String(uID),
handleType: "generic",
hasVideo: isVideo,
localizedCallerName: callerName,
fromPushKit: true
)
completion()
}
// Register for VoIP notifications
func voipRegistration(){
// Create a push registry object
let voipRegistry: PKPushRegistry = PKPushRegistry(queue: DispatchQueue.main)
// Set the registry's delegate to self
voipRegistry.delegate = self
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [PKPushType.voIP]
}
}
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if #available(iOS 14.0, *) {
completionHandler([ .banner, .alert, .sound, .badge])
} else {
completionHandler([.alert, .sound, .badge])
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print(deviceToken)
Messaging.messaging().apnsToken = deviceToken;
}
Flutter main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await initializeDateFormatting();
setupLocator();
var fcmService = locator<FCMService>();
FirebaseMessaging.onBackgroundMessage(FCMService.handleFirebaseBackgroundMessage);
FirebaseMessaging.onMessage.listen((event) {
print("Foreground message");
Fluttertoast.showToast(msg: "Received onMessage event");
FCMService.processCallNotification(event.data);
});
FirebaseMessaging.onMessageOpenedApp.listen((event) {
print("On message opened app");
Fluttertoast.showToast(msg: "Received onMessageOpenedAppEvent");
FCMService.handleInitialMessage(event);
});
FirebaseMessaging.instance.getInitialMessage().then((value) {
Fluttertoast.showToast(msg: "Received onLaunch event");
if (value != null) {
FCMService.handleInitialMessage(value);
}
});
initConnectyCube();
runApp(AppProviders());
}
FCMService.dart
// handle any firebase message
static Future<void> handleFirebaseBackgroundMessage(RemoteMessage message) async {
print("Received background message");
Fluttertoast.showToast(msg: "Received Firebase background message");
await Firebase.initializeApp();
setupLocator();
var fcmService = locator<FCMService>();
fcmService.init();
_handleMessage(message, launchMessage: true);
}
Testing:
测试是在 2 部实体 iPhone(6s 和 8)上完成的。当使用(调试和发布)模式直接从 Mac(Android Studio 和 XCode)构建时,两者都可以与 Firebase FCM 配合使用。从 TestFlight 下载相同的内容时,两者都不起作用。
如果任何人可以提供有关错误配置、设置错误或丢失/不正确的 Swift 代码,或者仅仅是错误或遗漏的见解,我们将不胜感激。