微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

找不到“ UNUserNotificationCenterDelegate”的协议声明

如何解决找不到“ UNUserNotificationCenterDelegate”的协议声明

我有一个使用Firebase进行通知的iOS应用。通知已设置并正常工作,我现在需要接收/处理通知以相应地显示视图控制器。我使用Objective C代码调用我的C ++代码,因此我的项目中有一个桥接头,该头主要是用Swift编写的。

我已经使用了Firebase文档中的this example(以及其他示例)。简而言之:遵守协议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

-更新

经过反复试验,似乎注释掉了从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.frameworkFrameworks,Libraries and Embedded Content中。
  • UserNotifications.frameworkLink 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。