为啥重新安装我的 iOS 应用后 Firebase Messaging 无法正常工作?
Posted
技术标签:
【中文标题】为啥重新安装我的 iOS 应用后 Firebase Messaging 无法正常工作?【英文标题】:Why Firebase Messaging is not working after reinstalling my iOS app?为什么重新安装我的 iOS 应用后 Firebase Messaging 无法正常工作? 【发布时间】:2016-12-27 01:24:47 【问题描述】:我刚刚正确实现了 Firebase,它运行良好,直到我卸载应用程序并从 Xcode 再次运行。从那时起,它不会收到任何 Firebase 通知,无论是后台还是前台。怎么可能?所有的证书似乎都可以。这是我的 AppDelegate.m:
@import Firebase;
@import FirebaseInstanceID;
@import FirebaseMessaging;
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Override point for customization after application launch.
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
// Use Firebase library to configure APIs
[FIRApp configure];
// Managing notifications:
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0"))
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
if(!error)
[self registerForNotification];
];
else
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
// ios 8 Notifications:
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
[self registerForNotification];
else
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
return YES;
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
NSString * deviceTokenString = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"The generated device token string is : %@",deviceTokenString);
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
NSLog(@"Failed to get token, error: %@", error.description);
// To receive notifications for iOS 9 and below.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
// Print full message.
NSLog(@"%@", userInfo);
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
[application registerForRemoteNotifications];
- (void)registerForNotification
UIApplication *application = [UIApplication sharedApplication];
// iOs 8 or greater:
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
UIMutableUserNotificationAction *open;
open = [[UIMutableUserNotificationAction alloc] init];
[open setActivationMode:UIUserNotificationActivationModeBackground];
[open setTitle:NSLocalizedString(@"View", nil)];
[open setIdentifier:NotificationActionOpenView];
[open setDestructive:NO];
[open setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryOpenView];
[actionCategory setActions:@[open]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
else if ([application respondsToSelector:@selector(registerForRemoteNotificationTypes:)])
// iOs 7 or lesser:
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
谢谢!
【问题讨论】:
您是通过控制台还是从您自己的应用服务器发送通知? 通过 Firebase 控制台 【参考方案1】:代码很好,没有错误。这似乎是与谷歌服务器或其他东西的同步问题。它发生在我身上,它在安装应用程序 1 小时后开始工作。
等一下 :)
【讨论】:
【参考方案2】:以防万一可以使某人受益,缺少的部分是:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
// for development
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
// for production
// [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];
【讨论】:
以上是关于为啥重新安装我的 iOS 应用后 Firebase Messaging 无法正常工作?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 Firebase/Auth 包没有安装在 xcode(swift 5) 中?
iOS MagicalRecord 之谜。为啥在 truncateAll 后重新启动时我的数据会重新出现?
为啥将 iOS 升级到 10.3 后 firebase 通知不再起作用?
为啥 Firebase 控制台在 SDK 已与我的 Android 和 iOS 应用程序集成时要求我实施 SDK?