1
votes

I am using Firebase Cloud Messaging in my app. It worked fine for a few weeks, but these last two days, it doesn't.

I send the messages from the Firebase Console. I handle the token refresh. What can be the problem?

This is my code:

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

Here is where I register for Push Notifications:

-(void)registerForPush
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
    // iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    UNAuthorizationOptions authOptions =
    UNAuthorizationOptionAlert
    | UNAuthorizationOptionSound
    | UNAuthorizationOptionBadge;

    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error)
    {
        if (error)
        {
            NSLog(@"\n\n %@ \n\n",error.description);
        }

        NSLog(@"");

    }];

    // For iOS 10 display notification (sent via APNS)
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    // For iOS 10 data message (sent via FCM)
    [FIRMessaging messaging].remoteMessageDelegate = self;
#endif
}


[[UIApplication sharedApplication] registerForRemoteNotifications];
}

Did register for Remote Notifications with Device Token:

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token = [[FIRInstanceID instanceID] token];
    NSLog(@"%@", token);

   // Add observer to listen for the token refresh notification.
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTokenRefresh) name:kFIRInstanceIDTokenRefreshNotification object:nil];

    if(token)
    {
      [self subscribeToTopics];
    }
}

this the Observer for when firebase refresh token :

- (void)onTokenRefresh
{
   // Get the default token if the earlier default token was nil. If the we already
   // had a default token most likely this will be nil too. But that is OK we just
   // wait for another notification of this type.
   NSString *token = [[FIRInstanceID instanceID] token];

   if (token)
   {
       [self subscribeToTopics];
   }
}

this is my subscribe to firebase topics :

-(void)subscribeToTopics
{
   [[FIRMessaging messaging] subscribeToTopic:@"/topics/ios"];
   [[FIRMessaging messaging] subscribeToTopic:@"/topics/all"];

   #ifdef DEBUG
   [[FIRMessaging messaging] subscribeToTopic:@"/topics/developer_devices"];
   #else
   [[FIRMessaging messaging]   unsubscribeFromTopic:@"/topics/developer_devices"];
   #endif
}
1

1 Answers

1
votes

Your APNs certificate has probably been revoked for some reason. Try generating a new one and re-uploading it in the firebase console!

https://firebase.google.com/docs/notifications/ios/console-audience#upload_your_apns_certificate