Search code examples
iosapple-push-notificationsdevicetoken

When application is re-installed How to get devicetoken on iOS8


I couldn't get devicetoken when i re-install our application on iOS8-adhoc-build.

When i re-install our application,I am calling the following code on application init.

func setAPNS(){
    let apnsTypes: UIUserNotificationType = [UIUserNotificationType.Badge,UIUserNotificationType.Sound,UIUserNotificationType.Alert]

    let notiSettings = UIUserNotificationSettings(forTypes:apnsTypes, categories:[])
    UIApplication.sharedApplication().registerUserNotificationSettings(notiSettings)
    UIApplication.sharedApplication().registerForRemoteNotifications()
}

After calling above code, 'didFailToRegisterForRemoteNotificationsWithError' is called. So I couldn't get devicetoken.

By the way,when didFailToRegisterForRemoteNotificationsWithError is called,I try to test the following code

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    //        print("Failed to get token, error: \(error)")
    CLSLogv("Log didFailToRegisterForRemoteNotificationsWithError %@", getVaList([error.description]))
    self.int_unregist += 1
    showLogMessage(NSStringFromClass(self.dynamicType),method:__FUNCTION__, message: error)
    if self.int_unregist <= 1{
        UIApplication.sharedApplication().unregisterForRemoteNotifications()
        self.setAPNS()

    }

}

But I couldn't still get devicetoken.

It's only iOS8-adhoc-build problem. It never happened on debug build.

I could get the following log from adhoc build.

Log didFailToRegisterForRemoteNotificationsWithError Error Domain=NSCocoaErrorDomain Code=3000 "Appの有効な“aps-environment”エンタイトルメント文字列が見つかりません" UserInfo=0x174664700 {NSLocalizedDescription=Appの有効な“aps-environment”エンタイトルメント文字列が見つかりません}

How do i resolve?


Solution

  • Looks like you doesn't generate push certificates for production environment, and you have it only for development environment (which is used only for builds launched from xcode, other build types will use production environment). So, just generate push certificates for production environment in developer center and regenerate your ad-hoc provision profile.