iospush-notificationdevicetoken

get device token without enter to the function didRegisterForRemoteNotificationsWithDeviceToken


I have in the settings page a button to let the user enable or disable the push notification.

If the user doesn't allow to receive push notification the first time he launch the application, how can I get the Device Token if the user turn on the notification setting later in setting page?

Thanks.


Solution

  • No its not possible , when user authenticate the allow permission and run your project , it automatically call the method didRegisterForRemoteNotificationsWithDeviceToken else it call the didFailToRegisterForRemoteNotificationsWithError

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:
    (NSData *)deviceToken {
    
    NSString* deviceTokene = [[[[deviceToken description]
                                stringByReplacingOccurrencesOfString: @"<" withString: @""]
                               stringByReplacingOccurrencesOfString: @">" withString: @""]
                              stringByReplacingOccurrencesOfString: @" " withString: @""] ;
    
    
    
    NSLog(@"%@",deviceTokene);
    
    
    }
    

    this is the coding to enable the APNS in Viewcontroller

      UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge
                                                                                                 |UIUserNotificationTypeSound
                                                                                                 |UIUserNotificationTypeAlert) categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    

    if you want to disable the APNS in inside the VC

     [[UIApplication sharedApplication] unregisterForRemoteNotifications];