【问题标题】:How to add Notification Service Extension for Firebase?如何为 Firebase 添加通知服务扩展?
【发布时间】:2019-08-11 00:59:47
【问题描述】:

我正在尝试按照此处的指南进行操作:https://firebase.google.com/docs/cloud-messaging/ios/send-image

我去了New > Target > Notification Service Extension(把这个新的target嵌入到原来的target中)并粘贴在NotificationService.m

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    // Modify the notification content here...
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];

    // Call FIRMessaging extension helper API.
    [[FIRMessaging extensionHelper] populateNotificationContent:self.bestAttemptContent
                                             withContentHandler:contentHandler];

    self.contentHandler(self.bestAttemptContent);
}

- (void)serviceExtensionTimeWillExpire {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    self.contentHandler(self.bestAttemptContent);
}

@end

我现在收到Use of undeclared identifier 'FIRMessaging' in NotificationService.m

NotificationService.m 中,我尝试导入类似于我的原始目标的AppDelegate.m,其中FIRMessaging 可用并且没有问题:

#import "NotificationService.h"
#import <Firebase.h>
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"
...

然后我得到'Firebase.h' file not found。我很困惑,因为它似乎在原始目标中工作,并且此通知服务扩展嵌入在该目标中。我试过弄乱Header Search PathsFramework Search Paths,但运气不佳。我做错了什么?

【问题讨论】:

  • 您是否在 podfile 中为“服务扩展”添加了 Firebase?
  • 哦,我不知道我的 Podfile 中还需要另一个目标!这很有效,让我克服了导入错误。现在我用FirMessagingRemoteNotificationsProxy.msharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead."
  • 酷。您在哪一行收到此错误?
  • NSObject&lt;UIApplicationDelegate&gt; *appDelegate = [[UIApplication sharedApplication] delegate]; 我读过 Firebase 将其升级为不使用 sharedApplication: github.com/firebase/firebase-ios-sdk/pull/1503,但我被困在旧版本 5.4.0 上。 :C
  • 不,您不能从“服务扩展”访问 AppDelegate。我也尝试用它来更新推送通知的批量计数,但没有用。

标签: ios objective-c firebase react-native firebase-cloud-messaging


【解决方案1】:

您只需在现有的 podfile 中添加以下文本。

target 'NotificationImage' do
   pod 'Firebase/Messaging'
end

只有您才能看到要包含在项目中的文件。

【讨论】:

  • 不生成 NotificationService.m 文件?我该怎么办?手动创建此文件或其他内容。请帮忙。
【解决方案2】:

放入你的 podfile

target 'yourNotificationServiceTarget' do
   pod 'Firebase/Messaging'
end

之后,放入你的 NotificationService.m

@import Firebase;

【讨论】:

    【解决方案3】:

    (1) 在您的 podfile 中添加 'Firebase/Messaging'。不要在您的应用扩展目标中添加不必要的 pod,因为 Apple 在应用扩展中不允许使用某些 API,例如UIApplication.shared。否则你可能会遇到很多错误。

    target 'NotificationServiceExtension' do
       pod 'Firebase/Messaging'
    end
    

    (2) 关闭项目并运行pod install

    查看this 教程,了解如何将 pod 添加到多个目标。

    【讨论】:

      【解决方案4】:

      您必须先安装 'Firebase/Messaging' pod。
      然后将以下内容添加到 pod 文件的底部。

      target 'NotificationImage' do
        inherit! :search_paths
      end
      

      如果不行,请按以下方式写:

      #Uncomment the following line if you want to add a global app version
      #platform :ios, '10.0'
      
      def shared_pods
        # your pods here
      end
      
      target 'YourProjectName' do
        # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
        use_frameworks!
        shared_pods
      
        target 'YourNotificationExtensionName' do
          inherit! :search_paths
        end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-20
        • 2017-05-13
        • 1970-01-01
        • 1970-01-01
        • 2017-05-16
        • 1970-01-01
        相关资源
        最近更新 更多