ios - 使用 Xcode/Objective C 订阅 FCM 主题时出现问题

标签 ios objective-c firebase firebase-cloud-messaging

我有一个我一直无法解决的问题。我对 iOS/Xcode 的了解有限,请见谅。

我正在尝试使用 xcode/Objective C 为 iOS 应用程序订阅 FCM 主题,但是在加载应用程序时,它没有订阅该主题,在调试器中它甚至看起来都不像在尝试。

如果我移动 [[FIRMessaging messaging] subscribeToTopic:@"/topics/channel"]; 行到正下方

[FIRMessaging messaging].delegate = self;
[application registerForRemoteNotifications];
[FIRApp configure];

(以及上面的 return YES;)然后至少我收到这条错误消息,表明它至少正在尝试。

2018-01-04 04:00:18.723760+0000 App[1049:1251125] [Firebase/Messaging][I-FCM002010] Cannot subscribe to topic: /topics/channel with token: (null)

所以按照建议here (以及许多其他地方),我试图将它移出到 didRegisterUserNotificationSettings 中,但它似乎根本没有做任何事情,调试器中也没有它的踪迹。

这是我的完整 AppDelegate.m 代码

//
//  AppDelegate.m
//

#import "AppDelegate.h"
#import <UserNotifications/UserNotifications.h>
#import <Firebase/Firebase.h>
#import <FirebaseMessaging/FirebaseMessaging.h>
#import <FirebaseInstanceID/FirebaseInstanceID.h>
#import <AFNetworking/AFNetworking.h>
@import Firebase;
@interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setupNetworkReachability];

    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error)
    {

    }];

    [FIRMessaging messaging].delegate = self;
    [application registerForRemoteNotifications];
    [FIRApp configure];
    //[[FIRMessaging messaging] subscribeToTopic:@"/topics/channel"];
    return YES;
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UNNotificationSettings *)notificationSettings
{
    [[FIRMessaging messaging] subscribeToTopic:@"/topics/channel"];
    NSLog(@"Topic Registered");
}

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

}

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

}

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

}

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

}

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

}

#pragma mark - Public Methods

+ (AppDelegate *)sharedAppDelegate
{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (void)setupNetworkReachability
{
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
     {
         if (status == AFNetworkReachabilityStatusReachableViaWiFi || status == AFNetworkReachabilityStatusReachableViaWWAN)
             self.isNetworkAvailable = YES;
         else
             self.isNetworkAvailable = NO;

         NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
     }];

    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

#pragma mark - FIRMessagingDelegate Methods

- (void)messaging:(nonnull FIRMessaging *)messaging didReceiveMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage
{
    NSLog(@"%@", remoteMessage.appData);
}

- (void)applicationReceivedRemoteMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage
{
    NSLog(@"%@", remoteMessage.appData);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
    NSLog(@"%@", notification.request.content.userInfo);
    completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);
}

@end

非常感谢任何帮助。谢谢。

只是为了更新,应用程序已经直接从 FCM 控制台接收消息,所以这部分工作正常,只是主题订阅失败了。

最佳答案

1:您应该在 [FIRMessaging messaging].delegate = self;

之前调用 [FIRApp configure];

2:当您发布AppDelegate.m 文件的完整代码时。所以它缺少以下方法来更新/发送 token 到 Firebase。您将在此 UIApplication 的委托(delegate)方法中收到一个 token ,您必须为 Firebase 设置它(只需将此方法放入您的 AppDelegate.m 文件中)

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

    NSString *strDevicetoken = [[NSString alloc]initWithFormat:@"%@",[[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""]];

    NSLog(@"Device Token = %@",strDevicetoken);
    [FIRMessaging messaging].APNSToken = deviceToken;
    //NSString *fcmToken = [FIRMessaging messaging].FCMToken;
    //NSLog(@"FCM registration token: %@", fcmToken);
}

现在您已经在 Firebase 成功设置了 APNS Token,您现在可以在 Firebase 订阅您想要的主题了

关于ios - 使用 Xcode/Objective C 订阅 FCM 主题时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48088532/

相关文章:

javascript - 有什么方法可以知道何时在 Firebase 数据库中插入和更新数据?

ios - 具有深度测试的 OpenGL 点 Sprite - 混合问题?

ios - Swift...在运行时更改 .js 文件的变量

IOS - 如何在注销时禁用推送通知?

iphone - objective-c 中的后台任务

ios - 如何实现 Firebase Listener 来观察数据库中的实时变化

firebase - 克隆 firebase 配置时权限被拒绝

ios - 更改 "Return"键盘按钮的文本

iphone - 如何设置一个tableview delegate

ios - 如何使用框架调用 UIViewController 类