问题描述
我有一个使用Firebase进行通知的iOS应用。通知已设置并正常工作,我现在需要接收/处理通知以相应地显示视图控制器。我使用Objective C代码来调用我的C ++代码,因此我的项目中有一个桥接头,该头主要是用Swift编写的。
我已经使用了Firebase文档中的this example(以及其他示例)。简而言之:遵守协议UNUserNotificationCenterDelegate
并实现其功能。我也在我的import UserNotifications
课堂上做AppDelegate
。
现在,在进行这几处更改时,我会遇到这两个错误
找不到“ UNUserNotificationCenterDelegate”的协议声明
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
-更新
经过反复试验,似乎注释掉了从objC到Swift的所有调用以及声明为@objc
的Swift类型的所有用法,使我的代码得以编译,并且桥接头不再抱怨。这还包括在我所有的Objective C代码中注释掉#import "myapp-Swift.h"
(这可能就是桥接标头不再抱怨的原因)。不幸的是,在ObjC中停止使用Swift类型是不可行的,因为对于一个看似很小的更改,它需要大量重写。
尽管我仍然不确定为什么或如何影响UNUserNotificationCenterDelegate
协议,我想这可能可以说明问题的根源。
-结束更新
其他注意事项:
- 该错误源自我导入生成的
myapp-Swift.h
的Objective C ++文件。 - 我尝试将
#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
中。 - 我尝试将部署目标更改为较新版本,但是没有运气。
- 我看过this question,它与本章基本相同,但没有任何效果。它进一步指向
- this question,但这不是我的用例。
这里是我的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,.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"
解决方法
挖掘一段时间后,我找到了错误的根源。简单地做
#import <UserNotifications/UserNotifications.h> // Added this
#import "myapp-Swift.h"
使代码编译。造成这种情况的原因是由于对桥接头的作用感到困惑。我相信,桥接标头都是用于从Objective C调用Swift ,反之亦然。事实并非如此。
-
myapp-Bridging-Header.h
向Swift公开了Objective C代码,以便能够从Swift调用到Objective C。 -
myapp-Swift.h
是自动生成的,并将标有@objc
的所有Swift代码公开给Objective C,以便能够从Objective C调用Swift。
由于UNUserNotificationCenterDelegate
协议的类型为NSObjectProtocol,因此协议声明位于目标C中。因此,除非完成myapp-Swift.h
,否则生成的#import <UserNotifications/UserNotifications.h>
对此一无所知第一。在这种情况下,甚至不需要在Swift中执行import UserNotifications
。