Krø*_*lle 3 apple-push-notifications ios firebase swift bridging-header
我有一个使用 Firebase 进行通知的 iOS 应用程序。通知已设置并正在运行,我现在需要接收/处理通知以相应地呈现视图控制器。我使用 Objective C 代码来调用我的 C++ 代码,因此我的项目中有一个桥接头,它主要是用 Swift 编写的。
我使用了firebase 文档中的这个示例(以及其他示例)。简而言之:遵守协议UNUserNotificationCenterDelegate并实现其功能。import UserNotifications我在班上也这样做AppDelegate。
现在,当进行这几个更改进行编译时,我收到这两个错误
找不到“UNUserNotificationCenterDelegate”的协议声明
未知类型名称“UNNotificationPresentationOptions”
对于生成的代码:
SWIFT_AVAILABILITY(ios,introduced=10)
@interface AppDelegate (SWIFT_EXTENSION(myapp)) <UNUserNotificationCenterDelegate>
- (void)userNotificationCenter:(UNUserNotificationCenter * _Nonnull)center willPresentNotification:(UNNotification * _Nonnull)notification withCompletionHandler:(void (^ _Nonnull)(UNNotificationPresentationOptions))completionHandler;
- (void)userNotificationCenter:(UNUserNotificationCenter * _Nonnull)center didReceiveNotificationResponse:(UNNotificationResponse * _Nonnull)response withCompletionHandler:(void (^ _Nonnull)(void))completionHandler;
@end
Run Code Online (Sandbox Code Playgroud)
经过一番尝试和错误后,似乎注释掉从 objC 到 Swift 的所有调用以及声明为 的所有 Swift 类型的使用都可以使@objc我的代码编译,并且桥接标头不再抱怨。这还包括在我的所有 Objective C 代码中进行注释#import "myapp-Swift.h"(这可能就是桥接标头不再抱怨的原因)。不幸的是,停止在 ObjC 中使用 Swift 类型是不可行的,因为对于一个看似很小的改变来说,它需要相当多的重写。
我想这可能在某种程度上表明了问题的根源,尽管我仍然不确定为什么或如何影响协议UNUserNotificationCenterDelegate。
其他注意事项:
myapp-Swift.h.#import <UserNotifications/UNUserNotificationCenter.h>到我的桥接头。Xcode 不会抱怨包含,因此它确实找到了它,但没有帮助。做#import <UserNotifications/UserNotifications.h>也行不通。UNUserNotificationCenterDelegate我尝试同时遵循其AppDelegate本身和作为扩展。两种情况下的错误是相同的。.mm文件中,错误源自那里。UserNotifications.framework是在Frameworks, Libraries and Embedded Content.UserNotifications.framework是在Link Binary With Libraries.这是我git diff的供参考:
diff --git a/ios/myapp.xcodeproj/project.pbxproj b/ios/myapp.xcodeproj/project.pbxproj
index 1ac676e..ca3a814 100644
--- a/ios/myapp.xcodeproj/project.pbxproj
+++ b/ios/myapp.xcodeproj/project.pbxproj
@@ -1550,7 +1550,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = "";
- IPHONEOS_DEPLOYMENT_TARGET = 9.3;
+ IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
@@ -1601,7 +1601,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = "";
- IPHONEOS_DEPLOYMENT_TARGET = 9.3;
+ IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
diff --git a/ios/myapp/AppDelegate.swift b/ios/myapp/AppDelegate.swift
index a1c9543..1010f99 100644
--- a/ios/myapp/AppDelegate.swift
+++ b/ios/myapp/AppDelegate.swift
@@ -7,6 +7,7 @@
//
import UIKit
+import UserNotifications
import Firebase
@UIApplicationMain
@@ -21,6 +22,21 @@ class AppDelegate: UIResponder, UIApplicationDelegate
FirebaseInterface.initialize()
ShoppingListInterface.loadLastSavedShoppingList()
+
+ if #available(iOS 10.0, *) {
+ // For iOS 10 display notification (sent via APNS)
+ UNUserNotificationCenter.current().delegate = self
+
+ let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
+ UNUserNotificationCenter.current().requestAuthorization(
+ options: authOptions,
+ completionHandler: {_, _ in })
+ } else {
+ let settings: UIUserNotificationSettings =
+ UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
+ application.registerUserNotificationSettings(settings)
+ }
+
return true
}
@@ -73,3 +91,46 @@ class AppDelegate: UIResponder, UIApplicationDelegate
// TODO: Save ShoppingList
}
}
+
+@available(iOS 10, *)
+extension AppDelegate : UNUserNotificationCenterDelegate
+{
+
+ // Receive displayed notifications for iOS 10 devices.
+ func userNotificationCenter(_ center: UNUserNotificationCenter,
+ willPresent notification: UNNotification,
+ withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
+ let userInfo = notification.request.content.userInfo
+
+ // With swizzling disabled you must let Messaging know about the message, for Analytics
+ // Messaging.messaging().appDidReceiveMessage(userInfo)
+ // Print message ID.
+// if let messageID = userInfo[gcmMessageIDKey] {
+// print("Message ID: \(messageID)")
+// }
+
+ // Print full message.
+ print(userInfo)
+
+ // Change this to your preferred presentation option
+ completionHandler([[.alert, .sound]])
+ }
+
+ func userNotificationCenter(_ center: UNUserNotificationCenter,
+ didReceive response: UNNotificationResponse,
+ withCompletionHandler completionHandler: @escaping () -> Void) {
+ let userInfo = response.notification.request.content.userInfo
+ // Print message ID.
+// if let messageID = userInfo[gcmMessageIDKey] {
+// print("Message ID: \(messageID)")
+// }
+
+ // With swizzling disabled you must let Messaging know about the message, for Analytics
+ // Messaging.messaging().appDidReceiveMessage(userInfo)
+ // Print full message.
+ print(userInfo)
+
+ completionHandler()
+ }
+}
+// [END ios_10_message_handling]
diff --git a/ios/myapp/myapp-Bridging-Header.h b/ios/myapp/myapp-Bridging-Header.h
index 1b2d4c1..4973a15 100644
--- a/ios/myapp/myapp-Bridging-Header.h
+++ b/ios/myapp/myapp-Bridging-Header.h
@@ -11,6 +11,7 @@
myapp-Swift.h, remember to do #import <UIKit/UIKit.h> in the Objective C
header file.
*/
+#import <UserNotifications/UNUserNotificationCenter.h>
#import "ShoppingListWrapper.h"
#import "DBWrapper.h"
#import "FirebaseWrapper.h"
Run Code Online (Sandbox Code Playgroud)
经过一段时间的挖掘,我找到了错误的根源。简单地做
#import <UserNotifications/UserNotifications.h> // Added this
#import "myapp-Swift.h"
Run Code Online (Sandbox Code Playgroud)
使代码编译。其原因在于对桥接标头的作用的混淆。我相信桥接头文件既可以用于从 Objective C 调用到 Swift ,也可以用于从 Objective C 调用 Swift。不是这种情况。
myapp-Bridging-Header.h将 Objective C 代码公开给 Swift,以便能够从 Swift 调用 Objective C。myapp-Swift.h是自动生成的,并将标记为 的任何 Swift 代码公开@objc给 Objective C,以便能够从 Objective C 调用 Swift。由于UNUserNotificationCenterDelegate协议的类型为NSObjectProtocol,因此协议声明是在 Objective C 中。因此,生成的协议myapp-Swift.h不知道它,除非#import <UserNotifications/UserNotifications.h>先完成。import UserNotifications在这种情况下,甚至不需要使用 Swift进行操作。