9

I can not receive the device token when registering for remote notifications. I get the alert message "Do you want to allow App X to be able to send you notificaitons", but when I accept it, the didRegisterForRemoteNotifications function is not called. I tried the following code.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    var types: UIUserNotificationType = UIUserNotificationType.Badge |
        UIUserNotificationType.Alert |
        UIUserNotificationType.Sound

    var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )

    application.registerUserNotificationSettings( settings )
    application.registerForRemoteNotifications()

    return true
}

func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {

    var characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )

    var deviceTokenString: String = ( deviceToken.description as NSString )
        .stringByTrimmingCharactersInSet( characterSet )
        .stringByReplacingOccurrencesOfString( " ", withString: "" ) as String

    println( deviceTokenString )

}

My provisioning profile and certificates are in order.

Has someone else had this problem?

9
  • Have you implemented the callback for when registering fails and see if that gets called?
    – dan
    Oct 13, 2015 at 18:47
  • I applied delegate method when registering fails but it is not calling Oct 14, 2015 at 3:53
  • I have had this same problem did you ever figure it out? Jul 19, 2016 at 11:57
  • Your application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) is not calling? Jul 19, 2016 at 16:17
  • Correct. I have no problems with the certificates involved and it was working fine just the other day, none of the certificates are expired it just won't call and i tried all kinds of ways trying to call it Jul 19, 2016 at 17:37

3 Answers 3

2

So I was running my app on a device for a while and I came back to see that the application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) had been called out of blue and now it works like a charm! My best guess to why it is that this happened would be that it just took some time for the certificates and everything else that goes on in the background to configure push notifications to be configured. So for anyone that is having the same problem I suggest giving it some time and then coming back to it, in my case it took about 12 hours if its any help.

2

Sometimes the sandbox apns is down like it happened yesterday, at that time the delegates are not called for device token.

0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if([[UIDevice currentDevice] systemVersion].floatValue >= 8.0)
{
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];    
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge)];
}
}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
           //Your device token code
}

Try to implement below methods and check:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}

//For interactive notification only
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){
    }
    else if ([identifier isEqualToString:@"answerAction"]){
    }
}
2
  • 2
    Don't tell people to accept your answer. If the answer is helpful to the OP, they will accept. But if you read the OP's self-answer, you would see that your answer did not solve his problem.
    – JAL
    Jul 26, 2016 at 18:55
  • 2
    Also, the question is tagged "swift", not "objective-c". You should post Swift code. Please always follow the question's tags.
    – Eric Aya
    Jul 27, 2016 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.