简体   繁体   中英

how to get the users` device token for push notification

I am using push notification to inform users. I have tested successfully on real device. Now I try to integrated it into my app. I am wondering how to get the users` device token.

I think I could send the device token through http or ftp to my server and stored them in a db. The server is responsible to push notification to all the device which provide the token. Is this the right way to do the job or is there some better ways?

You can print device token using this code:

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 

    NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
    NSLog(str); 
}

For more information: CLICK HERE: Tutorial_Programming Apple Push Notification Services

Yes, you need to handle it server side along with the badge number you see next to application icons. You could use a PHP file (or whatever you are familiar with) that receives the token every time the user launches the app, and then sets the badge icon to 0 in the db. For every new notification you send, you increase that field by +1. Hope this is useful for you.

If you are still not getting device token, try putting following code so to register your device for push notification.

It will also work on ios8 or more.

if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000

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

} else

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM