0

I have a problem which I've been unable to resolve. My knowledge of iOS/Xcode is limited, please forgive me.

I am trying to subscribe to an FCM topic for an iOS app using xcode/Objective C but when loading the App, it is not subscribing to the topic, in the debugger it doesn't even look like it is trying.

If I move the [[FIRMessaging messaging] subscribeToTopic:@"/topics/channel"]; line to just below

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

(and above return YES;) then at least I get this error message indicating that it is at least trying.

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

So following the suggestion here (and a number of other places), I have tried to move it out into didRegisterUserNotificationSettings but it doesn't seem to be doing anything at all and there is no trace of it in the debugger.

This is my full AppDelegate.m code

//
//  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

Would appreciate any help. Thank you.

Just to update, the App already receives message directly from FCM console, so that part works OK, it is just the topic subscription that is failing.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
omega1
  • 1,031
  • 4
  • 21
  • 41
  • 1
    Shouldn't you call [FIRApp configure]; before [FIRMessaging messaging].delegate = self; Also Try calling subscribe after func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { – Pallavi Srikhakollu Jan 04 '18 at 06:28

1 Answers1

1

1: You should call [FIRApp configure]; before [FIRMessaging messaging].delegate = self;

2: As you post full code of your AppDelegate.m file. So it is missing the below method to update/send token to Firebase. You will receive a TOKEN with in this UIApplication's delegate method and you have to set it for Firebase (Just put this method in your AppDelegate.m file)

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

Now you have successfully set APNS Token at Firebase and you can now subscribe to your desired topic at Firebase

M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45