ios - NSInvalidArgumentException FIRMessaging connectWithCompletion
问题描述
在 Firebase for iOS 上创建项目,成功安装 pod,添加 GoogleService-info.plist,
启用推送通知,将 Auth 密钥添加到 firebase,添加 $(inhertied)Other C Flags
以及PODS ROOTS
APS 环境和钥匙串访问组的权利,
向委托添加了以下实现:
#import <Firebase/Firebase.h>
#import <FirebaseInstanceID/FirebaseInstanceID.h>
#import <FirebaseMessaging/FirebaseMessaging.h>
@implementation AppDelegate
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#endif
// Copied from Apple's header in case it is missing in some cases (e.g. pre-Xcode 8 builds).
#ifndef NSFoundationVersionNumber_iOS_9_x_Max
#define NSFoundationVersionNumber_iOS_9_x_Max 1299
#endif
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
application.applicationIconBadgeNumber = 0;
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7.1 or earlier. Disable the deprecation warnings.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIRemoteNotificationType allNotificationTypes =
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
[application registerForRemoteNotificationTypes:allNotificationTypes];
#pragma clang diagnostic pop
}
else
{
// iOS 8 or later
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
// For iOS 10 display notification (sent via APNS)
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
}
}
[FIRApp configure];
// Add observer for InstanceID token refresh callback.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
[self removeScreen];
[window makeKeyAndVisible];
return YES;
}
// With "FirebaseAppDelegateProxyEnabled": NO
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[FIRMessaging messaging] setAPNSToken: deviceToken type: FIRMessagingAPNSTokenTypeProd];
NSString *tokenString = [deviceToken description];
tokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
tokenId = [tokenString stringByReplacingOccurrencesOfString:@" " withString:@""];
self.devToken = tokenId;
[self connectToFcm];
}
- (void)tokenRefreshNotification:(NSNotification *)notification {
[self connectToFcm];
}
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Unable to connect to FCM. %@", error);
} else {
NSLog(@"Connected to FCM.");
}
}];
}
当我运行代码时,我收到以下错误:
由于未捕获的异常而终止应用程序'NSInvalidArgumentException', reason: '-[FIRMessaging connectWithCompletion:]: unrecognized selector sent to instance 0x280bc0c00'
您知道可能导致错误的原因以及如何解决吗?
解决方案
遵循@Eysner 提供的: https ://firebase.google.com/docs/cloud-messaging/ios/client
我替换了以下代码:
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Unable to connect to FCM. %@", error);
} else {
NSLog(@"Connected to FCM.");
}
}];
}
使用以下代码:
- (void)connectToFcm {
[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result, NSError * _Nullable error{
if (error != nil) {
NSLog(@"Error fetching remote instance ID: %@", error);
}
else {
NSLog(@"Remote instance ID token: %@", result.token);
NSString* message = [NSString stringWithFormat:@"Remote InstanceID token: %@", result.token];
}
}];
}
推荐阅读
- javascript - 从对象中获取值到数组中
- java - 将页面添加到 Spring Boot 应用程序(非常基础)
- javascript - Angular 6:重新加载窗口(刷新 F5)在 IE11 中需要一个额外的“/”来重新加载当前路径
- php - 没有链接参数的mysqli_query
- firebase - Firebase Auth API 电子邮件验证 JavaScript
- react-native - 已安装库但在 react-native 环境中不起作用
- scala - 如何加入数据框中的数据
- android - 从通知操作启动后台服务的白名单对开发人员真正意味着什么?
- php - 表单提交在mysql中显示一个空白条目
- angular - 第二次动作调度在 RxJS 和 Angular 6 中不起作用