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?

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • Have you implemented the callback for when registering fails and see if that gets called? – dan Oct 13 '15 at 18:47
  • I applied delegate method when registering fails but it is not calling – Mahesh Kolagatla Oct 14 '15 at 03:53
  • I have had this same problem did you ever figure it out? – Garret Kaye Jul 19 '16 at 11:57
  • Your `application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData )` is not calling? – Reinier Melian Jul 19 '16 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 – Garret Kaye Jul 19 '16 at 17:37
  • @jimmy Did you implement `application:didFailToRegisterForRemoteNotificationsWithError` to see if an error is being returned? Are you testing on an actual device and not the simulator? – JAL Jul 19 '16 at 17:40
  • yes I am doing it on an actual device and i have implemented application:didFailToRegisterForRemoteNotificationsWithError but still not one thing is called – Garret Kaye Jul 19 '16 at 17:44
  • @MaheshKolagatla Device token is not available for simulator. Use real device you will get device token in `deviceToken` parameter – Kumar Jul 20 '16 at 08:57
  • @MaheshKolagatla [**setup push notifications in Swift**](http://stackoverflow.com/a/36172703/1378447) – Warif Akhand Rishi Jul 26 '16 at 10:36

3 Answers3

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.

Garret Kaye
  • 2,412
  • 4
  • 21
  • 45
2

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

JAL
  • 41,701
  • 23
  • 172
  • 300
Garima
  • 21
  • 4
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"]){
    }
}
JAL
  • 41,701
  • 23
  • 172
  • 300
Bhoomi Jagani
  • 2,413
  • 18
  • 24
  • 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 '16 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 '16 at 15:17