ios - 使用 firebase 和 Objective C 不接收推送通知

标签 ios objective-c firebase apple-push-notifications firebase-cloud-messaging

我正在尝试使用 Firebase 进行推送通知。我已通过 Cocoapods 成功安装了 FirebaseFirebase 消息传递

我使用了下面的代码

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [FIRApp configure];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshCallback:) name:kFIRInstanceIDTokenRefreshNotification object:nil];


    UIUserNotificationType allNotificationsType = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationsType categories:nil];
    [application registerUserNotificationSettings:settings];

    [application isRegisteredForRemoteNotifications];

    return YES;
}


 -(void)applicationDidEnterBackground:(UIApplication *)application {

    [[FIRMessaging messaging] disconnect];
    NSLog(@"Disconnect from FCM");
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
     [self connectToFirebase];
}

- (void) application:(UIApplication *) application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler{

    // Pring The Message Id
    NSLog(@"Mesage Id : %@",userInfo[@"gcm.message_id"]);

    // Print Full Message
    NSLog(@"%@",userInfo);
}

#pragma mark -- Custom Firebase code


- (void)tokenRefreshCallback:(NSNotification *) notification{
    NSString *refreshedToken = [[FIRInstanceID instanceID] token];
    NSLog(@"IstanceID token: %@", refreshedToken);

    // Connect To FCM since connection may have failed when attempt before having a token
    [self connectToFirebase];
}

-(void) connectToFirebase{

    [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error)
     {
         if ( error != nil)
         {
             NSLog(@"Unable to Connect To FCM. %@",error);
         }
         else
         {
             NSLog((@"Connected To FCM"));
         }
     }];
}

当我使用与 Xcode 连接的 iPhone 运行上述代码时,当我从 Firebase 控制台发送消息时,我遇到了以下问题

1).当应用程序处于前台(事件)时,日志显示以下消息

Mesage Id : (null){
    CustommData = "First Message";
    "collapse_key" = "abcd.iospush.FireBasePush";
    from = 555551391562;
    notification =     {
        badge = 4;
        body = "Test Message";
        e = 1;
        sound = default;
        sound2 = default;
    };
}

注意消息 IDnull

2).无论应用程序处于前台、后台还是关闭状态,我的手机在通知中心中均不显示任何通知

我希望用户在应用程序处于后台、前台或关闭时收到推送通知

最佳答案

您必须调用 [[UIApplication sharedApplication] registerForRemoteNotifications]; 才能正确注册远程通知。

为后台使用配置远程通知

您是否已将应用的后台模式配置为接受远程通知? 您可以通过点击:项目名称 -> 功能 -> 后台模式

打开它,然后勾选远程通知旁边的框,如下面的屏幕截图所示。

enter image description here

关于ios - 使用 firebase 和 Objective C 不接收推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39227220/

相关文章:

angular - 错误错误 : InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'

ios - TableViewController 使用静态单元格检测行点击

ios - 将 UIBezierPath 转换为字符串

ios - 如何在敌人不与它们互动的情况下产生能量提升并检测它们何时被拾取

iphone - UITextField 动画完成,但在用户触摸后又弹回到原来的位置

objective-c - Mac Mountain Lion 从 CLI 应用发送通知

iphone - 核心数据获取结果

iOS生成随机消息

javascript - Cloud Firestore 集合查询不起作用

ios - 检测Google用户之前是否登录过