本文介绍了未使用firebase和Objective C接收推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use Firebase for push notification. i have successfully installed the Firebase And Firebase messaging via Cocoapods.

我使用以下代码

 - (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 当我从Firebase控制台发送消息时,我面临以下问题

When i run the above code using my iPhone connected with Xcode i am facing the following issues when i send a message from Firebase Console

1)。当应用程序处于前台(活动)时,Log显示以下消息

1). when the app is in foreground (active), Log shows the below message

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;
    };
}

通知消息ID null

2)。我的手机未在通知中心中显示任何通知,无论应用是在前台,背景还是已关闭

2). My phone doesn't show any notification in Notification Centre whether App is in Foreground , Background or Closed

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

I want user to receive push notifications when App is in Background , Foreground or Closed

推荐答案

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

You must call [[UIApplication sharedApplication] registerForRemoteNotifications]; to properly register for remote notifications.

配置远程通知以供后台使用

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

Have you configured your app's background modes to accept remote notifications?You can do this by clicking: Project Name -> Capabilities -> Background Modes

启用此功能,然后勾选远程通知旁边的框,如下面的屏幕截图所示。

Turn this on and then tick the box beside Remote Notifications as seen in the screenshot below.

这篇关于未使用firebase和Objective C接收推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 15:42