如何在Swift中注册远程通知时获取设备令牌

时间:2015-10-13 18:12:51

标签: ios swift apple-push-notifications

注册远程通知时,我无法收到设备令牌。我收到了警告消息"Do you want to allow App X to be able to send you notificaitons",但是当我接受它时,不会调用didRegisterForRemoteNotifications函数。我尝试了以下代码。

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 )

}

我的配置文件和证书是有序的。

有其他人有这个问题吗?

3 个答案:

答案 0 :(得分:2)

所以我在设备上运行我的应用程序已经有一段时间了,我回来看看application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) 被称为蓝色,现在它就像一个魅力!我最好的猜测是,为什么会发生这种情况,因为它只需要一些时间来获取证书以及后台发生的所有其他事情来配置推送通知。因此,对于遇到同样问题的人,我建议给它一些时间,然后回到它,在我的情况下,如果它有任何帮助,它需要大约12个小时。

答案 1 :(得分:2)

有时沙箱apns会像昨天发生的那样下降,那时代理人不会被调用设备令牌。

答案 2 :(得分: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
}

尝试实施以下方法并检查:

- (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"]){
    }
}