简体   繁体   English

如何获取用户的设备令牌以进行推送通知

[英]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. 我想我可以通过http或ftp将设备令牌发送到我的服务器,并将它们存储在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 有关更多信息: 单击此处: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. 是的,您需要在服务器端处理它,并在应用程序图标旁边看到badge编号。 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. 您可以使用一个PHP文件(或您熟悉的任何文件),该文件在用户每次启动应用程序时都接收令牌,然后在数据库中将徽章图标设置为0。 For every new notification you send, you increase that field by +1. 对于您发送的每条新通知,您都将该字段增加+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. 它也可以在ios8或更高版本上使用。

if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 如果__IPHONE_OS_VERSION_MAX_ALLOWED> = 80000

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

} else }其他

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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