使用Xcode / Objective C

时间:2018-01-04 04:08:22

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

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

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

如果我移动[[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 个答案:

答案 0 :(得分:1)

1:您应在FIRApp configure];

之前致电[[FIRMessaging messaging].delegate = self;

2:当您发布AppDelegate.m文件的完整代码时。因此,缺少以下方法来更新/发送令牌到Firebase。您将在此UIApplication的委托方法中收到一个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令牌,现在可以在Firebase订阅所需主题