远程通知令牌处理 [英] Remote Notification Token handling

查看:20
本文介绍了远程通知令牌处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为远程通知注册我的 iPhone 应用程序.

I'm registering my iPhone application for Remote Notification.

这是我的场景:

  1. 用户打开我的应用并注册远程通知
  2. 用户为我的应用关闭通知
  3. 用户打开我的应用
  4. 用户为我的应用开启通知

我的问题是:

  1. 是否可以检查用户是否在我的应用代码中禁用了我的应用的远程通知?
  2. 如果我在没有用户关闭应用通知的情况下请求新令牌,检索到的令牌是不同的还是始终相同?
  3. 我是否应该在每次启动应用时请求一个新令牌?

推荐答案

那么您想了解 UIRemoteNotifications 吗?:) 很酷,因为它们并不像人们通常认为的那样复杂.不过,我将以相反的顺序回答您的问题.它比方式更流畅.

So you want to know about UIRemoteNotifications? :) Cool, because they're not really as complex as people often make them out to be. Though, I am going to address your questions in reverse order. It flows better than way.

我是否应该在每次启动应用时请求一个新令牌?

Should I request a new token every time I start my app?

有点.使用 UIRemoteNotifications,您永远不会真正请求令牌,而是请求许可并接收令牌.您应该做的是在您的应用委托中实现 application:didRegisterForRemoteNotificationsWithDeviceToken:.这个方法(连同它的错误捕获兄弟 application:didFailToRegisterForRemoteNotificationsWithError:)是 registerForRemoteNotificationTypes: 的回调.最佳做法是在 application:didFinishLaunchingWithOptions: 期间调用 registerForRemoteNotificationTypes:.(不要担心所有的方法名称到处乱飞.我将很快解释代码方式).

Sort of. With UIRemoteNotifications, you never really request a token, so much as request permission and receive a token. What you should do is implement application:didRegisterForRemoteNotificationsWithDeviceToken: in your app delegate. This method (along with its error-catching sibling application:didFailToRegisterForRemoteNotificationsWithError:) is the callback for registerForRemoteNotificationTypes:. It's best practice to call registerForRemoteNotificationTypes: during application:didFinishLaunchingWithOptions:. (Don't worry about all of the method names flying around. I'll explain codewise shortly).

如果我在没有用户关闭应用通知的情况下请求新令牌,检索到的令牌是不同的还是始终相同?

If I request a new token without user turn off notification for my app, the retrieved token is different or is always the same?

也许吧.出于安全原因,设备令牌可能会发生变化,但通常您不必担心它会发生变化.

Maybe. The device token is subject to change for security reasons, but in general you shouldn't need to be too concerned with it changing.

是否可以检查用户是否在我的应用代码中禁用了我的应用的远程通知?

Is it possible to check if the user has disabled remote notification for my app inside my app's code?

为什么,是的.UIApplicationDelegate 有一个名为 enabledRemoteNotificationTypes 的方法,它获取您请求并由您的用户启用的所有远程通知类型.稍后会详细介绍.

Why, yes it is. UIApplicationDelegate has a method called enabledRemoteNotificationTypes, which gets all of the remote notification types requested by you and enabled by your user. More on that shortly.

归根结底,你应该得到这样的结果:

At the end of the day, you should end up with something like this:

#define deviceTokenKey   @"devtok"
#define remoteNotifTypes UIRemoteNotificationTypeBadge | \
                         UIRemoteNotificationTypeAlert

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    //generic setup and whatnot

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: remoteNotifTypes];
    if (([[NSUserDefaults standardUserDefaults] stringForKey: deviceTokenKey]) &&
        ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] != remoteNotifTypes))
    {
        //user has probably disabled push. react accordingly.
    }
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token
{    
    NSString *deviceToken = [token description];
    deviceToken = [deviceToken stringByReplacingOccurrencesOfString: @"<" withString: @""];
    deviceToken = [deviceToken stringByReplacingOccurrencesOfString: @">" withString: @""];
    deviceToken = [deviceToken stringByReplacingOccurrencesOfString: @" " withString: @""];
    
    if ([[NSUserDefaults standardUserDefaults] stringForKey: deviceTokenKey])
    {
        if (![[[NSUserDefaults standardUserDefaults] stringForKey: deviceTokenKey] isEqualToString: deviceToken])
        {
            [[NSUserDefaults standardUserDefaults] setObject: deviceToken forKey: deviceTokenKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
    
            //user allowed push. react accordingly.
        }
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setObject: deviceToken forKey: deviceTokenKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
        //user allowed push. react accordingly.
    }
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog("application: %@ didFailToRegisterForRemoteNotificationsWithError: %@", application, [error localizedDescription]);
}

这篇关于远程通知令牌处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆