简体   繁体   English

如何以Apple Push Notification Service提供商的身份获取设备令牌?

[英]How to get device tokens as Apple Push Notification Service provider?

I would like to add support for sending Apple Push Notifications to a iOS app. 我想增加对将Apple Push Notifications发送到iOS应用程序的支持。 This is a news app, so every time some important news event has occurred, then I would like to broadcast a push notification to all users and devices where the app is installed. 这是一个新闻应用程序,因此每次发生一些重要新闻事件时,我都想向安装该应用程序的所有用户和设备广播推送通知。

According to the Apple Push Notification Service documentation , in Figure 3-3 the Client App must send the device token to the Provider. 根据Apple Push Notification Service文档 ,在图3-3中,客户端应用程序必须将设备令牌发送给提供商。

In order to be able to send Push Notifications to all devices where my app is installed, do I have to create a webservice which receives and stores device tokens from the client apps when they register for notifications? 为了能够将推送通知发送到安装了我的应用程序的所有设备,我是否必须创建一个Web服务来在客户端应用程序注册通知时从客户端应用程序接收并存储设备令牌? In order to send a push notification, I need a token and the payload. 为了发送推送通知,我需要令牌和有效负载。 I want to send a push notification to all users, typically many thousand users. 我想向所有用户(通常是数千个用户)发送推送通知。 So how can I best get access to the device tokens so that I can send the push notification to all users? 那么,如何才能最好地访问设备令牌,以便将推送通知发送给所有用户?

Yes. 是。 You will have to make a web service for sending the push notifications to any of the devices that are using your app. 您将必须提供一个Web服务,用于将推送通知发送到使用您的应用程序的任何设备。 As far as the device tokens are concerned, you can get them through your code using this : 就设备令牌而言,您可以使用以下代码通过代码获取它们:

- (void)applicationDidFinishLaunching:(UIApplication *)app {
   // other setup tasks here....
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    const void *devTokenBytes = [devToken bytes];
    self.registered = YES;
    [self sendProviderDeviceToken:devTokenBytes]; // custom method
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSLog(@"Error in registration. Error: %@", err);
}

You need to write this in your AppDelegate.m , this will fetch the device token needed to send the push notifications. 您需要在AppDelegate.m编写此代码,这将获取发送推送通知所需的设备令牌。

Also, you can store those tokens on some Database server for further usage in sending notifications. 另外,您可以将这些令牌存储在某些数据库服务器上,以供进一步用于发送通知。 You can use these tokens the next time when you want to send some notifications to selected or all of the users. 当您想向选定或所有用户发送一些通知时,下次可以使用这些令牌。

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

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