Apple Push Notifications: Not receiving device token?

烈酒焚心 提交于 2019-12-11 14:59:43

问题


In an app which involves push notifications, I am receiving device tokens just fine but the my client's device doesn't receive device token (hence causing failure to receive push notifications). I am using Parse for push notifications. This is what I have in my AppDelegate:

-(void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:newDeviceToken];
    PFACL *objectACL = [[PFACL alloc] init];
    [objectACL setPublicReadAccess:YES];
    [objectACL setPublicWriteAccess:YES];

    [currentInstallation saveInBackground];
}

-(void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{

    NSString *errorString = [NSString stringWithFormat:@"%@", error];
    UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

    [errorAlertView show];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [Parse setApplicationId:PARSE_APPLICATION_ID
                  clientKey:PARSE_CUSTOMER_KEY];
    [PFFacebookUtils initializeFacebook];

    [self.window makeKeyAndVisible];

    // create the Login controller instance:

    [application registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeSound];
}

Client doesn't receive any UIAlert representing failure of registration with APN so that means didFailToRegisterForRemoteNotificationsWithError is not being called. I have tried removing all installations from my parse server, removing the app & reinstalling again as per guidelines of Apple technical note TN2265. The application doesn't prompt the client for authorization of push notifications on first launch & doesn't appear in notification center either (Which is baffling because didRegisterUserNotificationSettings: is there too). But all this is working fine on my iPad, iPhone 5S, iPod 5, iPhone 4s. Any ideas what Might be causing this device to device variable behavior in the app?!This is the sc client sent me.notificationCenterScreenShot


回答1:


Try following code

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }

within - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method in your AppDelegate class



来源:https://stackoverflow.com/questions/27729129/apple-push-notifications-not-receiving-device-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!