为什么重新安装iOS应用程序后Firebase消息无法正常工作?
我刚刚正确地实现了Firebase,在我卸载应用程序并从Xcode再次运行之前,它工作得非常好。从那时起,它不会收到任何Firebase通知,无论是背景还是前景。怎么可能呢?所有的证书似乎都没问题。这是我的AppDelegate.m:为什么重新安装iOS应用程序后Firebase消息无法正常工作?,ios,objective-c,firebase,push-notification,firebase-cloud-messaging,Ios,Objective C,Firebase,Push Notification,Firebase Cloud Messaging,我刚刚正确地实现了Firebase,在我卸载应用程序并从Xcode再次运行之前,它工作得非常好。从那时起,它不会收到任何Firebase通知,无论是背景还是前景。怎么可能呢?所有的证书似乎都没问题。这是我的AppDelegate.m: @import Firebase; @import FirebaseInstanceID; @import FirebaseMessaging; @interface AppDelegate () @end @implementation AppDelega
@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;
@导入FirebaseInstanceID;
@导入FirebaseMessaging;
@接口AppDelegate()
@结束
@实现AppDelegate
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
//应用程序启动后自定义的覆盖点。
UIPageControl*pageControl=[UIPageControl外观];
pageControl.pageIndicatorTintColor=[UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor=[UIColor blackColor];
pageControl.backgroundColor=[UIColor whiteColor];
//使用Firebase库配置API
[FIRApp配置];
//管理通知:
如果(系统版本或同等版本(@“10.0”)){
UNUserNotificationCenter*中心=[UNUserNotificationCenter currentNotificationCenter];
center.delegate=self;
[center requestAuthorizationWithOptions:(未授权选项声音|未授权选项插入|未授权选项徽章)完成处理程序:^(BOOL已授予,N错误*_可为空错误){
如果(!错误){
[自我注册修饰];
}
}];
}否则{
if([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]){
//iOS 8通知:
[应用程序注册表通知设置:[UIUserNotificationSettings设置类型:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)类别:无];
[申请登记处证明];
[自我注册修饰];
}否则{
//iOS<8通知
[应用程序注册信息类型:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
}
返回YES;
}
-(无效)应用程序:(UIApplication*)应用程序DIdRegisterForRemotionTificationswithDeviceToken:(NSData*)deviceToken{
NSString*deviceToken安装=[[[deviceToken说明]
StringByReplacingOfString:@“with String:@”]
StringByReplacingOfString:@“和字符串:@]”发生;
NSLog(@“生成的设备令牌字符串为:%@”,deviceTokenString);
}
-(无效)应用程序:(UIApplication*)应用程序未能注册远程通知,错误为:(N错误*)错误{
NSLog(@“无法获取令牌,错误:%@”,错误。说明);
}
//接收iOS 9及以下版本的通知。
-(无效)应用程序:(UIApplication*)应用程序DidReceiveMemotentification:(NSDictionary*)用户信息
fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler{
//如果您在应用程序处于后台时收到通知消息,
//在用户点击启动应用程序的通知之前,不会触发此回调。
//TODO:处理通知的数据
//打印消息ID。
NSLog(@“消息ID:%@”,userInfo[@“gcm.Message_ID”);
//打印完整消息。
NSLog(@“%@”,userInfo);
}
-(void)应用程序:(UIApplication*)应用程序didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings{
[申请登记处证明];
}
-(无效)登记处通知{
UIApplication*application=[UIApplication sharedApplication];
//iOs 8或更高版本:
if([application respondsToSelector:@selector(registerUserNotificationSettings:)])){
UIMutableUserNotificationAction*打开;
open=[[UIMutableUserNotificationAction alloc]init];
[打开setActivationMode:UIUserNotificationActivationModeBackground];
[开放集标题:NSLocalizedString(@“视图”,无)];
[打开集合标识符:NotificationActionOpenView];
[开放日期:否];
[open setAuthenticationRequired:否];
UIMutableUserNotificationCategory*actionCategory;
actionCategory=[[UIMutableUserNotificationCategory alloc]init];
[actionCategory集合标识符:NotificationCategoryOpenView];
[actionCategory setActions:@[open]
forContext:UIUserNotificationActionContextDefault];
NSSet*类别=[NSSet setWithObject:actionCategory];
UIUserNotificationType=(UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings*设置;
设置=[UIUserNotificationSettings设置类型:类型
类别:类别];
[[UIApplication sharedApplication]注册表通知设置:设置];
}else if([application respondsToSelector:@selector(registerForRemoteNotificationTypes:)]){
//iOs 7或更低版本:
UIRemoteNotificationType myTypes=UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application RegisterForRemotionTificationTypes:myTypes];
}
}
谢谢!代码很好,没有错误。这似乎是谷歌服务器的同步问题或其他问题。这发生在我身上,在安装应用程序1小时后开始工作
请稍等:)以防万一,可能会使某人受益,缺少的部分是:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// for development
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
// for production
// [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];
}
您是通过控制台还是从自己的应用服务器发送通知?通过Firebase控制台