如何为Firebase添加Notification Service Extension?

时间:2019-08-11 00:59:47

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

我正在尝试遵循以下指南:https://firebase.google.com/docs/cloud-messaging/ios/send-image

我去了New> Target> Notification Service Extension(将这个新目标嵌入到原始目标中)并粘贴到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弄得一团糟。我在做什么错了?

4 个答案:

答案 0 :(得分:4)

放入您的Podfile

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

之后,放入您的NotificationService.m

@import Firebase;

答案 1 :(得分:2)

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

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

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

答案 2 :(得分:0)

(1)在您的Podfile中添加'Firebase/Messaging'。不要在您的App Extension目标中添加不必要的Pod,因为Apple在App Extension中不允许某些API,例如UIApplication.shared。否则,您可能会遇到很多错误。

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

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

请参阅this教程,以将Pod添加到多个目标。

答案 3 :(得分:-1)

您必须先安装 '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