简体   繁体   English

设备令牌和推送通知

[英]Device Token and Push Notification

I am trying to implement Push Notifications into my app, but I have, what I believe, may be a dumb question. 我正在尝试将推送通知应用到我的应用程序中,但我相信,我可能是一个愚蠢的问题。 I have my Push Notifications working, but they work because I am putting the Device Token into my asp.net code on the server. 我的推送通知有效,但它们有效,因为我将设备令牌放入服务器上的asp.net代码中。

My question is, am I supposed to keep track of the device tokens my app obtains? 我的问题是,我应该跟踪我的应用获得的设备令牌吗? In other words, when the app launches and I get the device token, do I need to send those up to my server and store them so that when I need to send a notification to all the users of my App i can go through the list of tokens and send the notification to each device token? 换句话说,当应用程序启动并且我获得设备令牌时,我是否需要将它们发送到我的服务器并存储它们以便当我需要向我的应用程序的所有用户发送通知时我可以通过列表令牌并将通知发送到每个设备令牌?

Thanks for any clarity you can bring, as you guessed this is my first attempt at Push Notifications. 感谢您提供的任何清晰度,因为您猜到这是我第一次尝试Push Notifications。

You have to store the device tokens in a database. 您必须将设备令牌存储在数据库中。 Then you send a notification addressed to each device token. 然后,您发送一个发送到每个设备令牌的通知。 You can create the system by yourself, but there are open source libraries that have done this already. 您可以自己创建系统,但是已经有这样做的开源库。 Although it is for PHP, Easy APNS is an example. 虽然它适用于PHP,但Easy APNS就是一个例子。

I'd run into this scenario myself, as it turns out hardcoding device tokens is one way to limit the devices which can be sent push notifications but if you wanted to allow any device who has downloaded your app you need to create a mechanism to send the device's device token to your server so that a push notification is sent to it. 我自己会遇到这种情况,因为事实证明硬编码设备令牌是限制可以发送推送通知的设备的一种方法但是如果你想允许任何下载你的应用程序的设备你需要创建一个机制来发送将设备的设备令牌发送到您的服务器,以便向其发送推送通知。 You could setup this request to your server on app load, and in my case silently succeed and on failure let the user know that they were not added to the notification list. 您可以在应用程序加载时将此请求设置到您的服务器,在我的情况下,默认成功并且在失败时让用户知道他们没有添加到通知列表中。

Here's an example from raywenderlich.com that creates a chat program. 这是一个创建聊天程序的raywenderlich.com的例子。 -- basically they are creating a post request with device info that they are storing in a database, and later accessing the DB info to iterate through and send out the notification. - 基本上他们正在创建一个带有设备信息的发布请求,他们将这些信息存储在数据库中,然后访问数据库信息以进行迭代并发送通知。

Some relevant code: 一些相关代码:

- (void)postJoinRequest
{
    MBProgressHUD* hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = NSLocalizedString(@"Connecting", nil);

    NSURL* url = [NSURL URLWithString:ServerApiURL];
    __block ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];

    [request setPostValue:@"join" forKey:@"cmd"];
    [request setPostValue:[dataModel udid] forKey:@"udid"];
    [request setPostValue:[dataModel deviceToken] forKey:@"token"];
    [request setPostValue:[dataModel nickname] forKey:@"name"];
    [request setPostValue:[dataModel secretCode] forKey:@"code"];

    [request setCompletionBlock:^
    {
        if ([self isViewLoaded])
        {
            [MBProgressHUD hideHUDForView:self.view animated:YES];

            if ([request responseStatusCode] != 200)
            {
                ShowErrorAlert(NSLocalizedString(@"There was an error communicating with the server", nil));
            }
            else
            {
                [self userDidJoin];
            }
        }
    }];

    [request setFailedBlock:^
    {
        if ([self isViewLoaded])
        {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            ShowErrorAlert([[request error] localizedDescription]);
        }
    }];

    [request startAsynchronous];
}

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

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