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

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

问题描述

我注册我的iPhone应用程序的远程通知。

下面我的情况:


  1. 用户打开我的应用程序并注册远程通知

  2. 用户开启关闭的通知我的应用程序

  3. 用户打开我的应用程序

  4. 用户打开通知我的应用程序

我的问题是:


  1. 是否有可能来检查,如果用户已禁用远程通知我的应用我的应用程序的code里面呢?

  2. 如果我没有要求用户关闭通知我的应用程序一个新的令牌,检索令牌是不同的,或者总是一样的?

  3. 我应该每次都请求一个新的令牌,我开始我的应用程序?


解决方案

所以,你想了解UIRemoteNotifications? :)酷,因为他们没有真正的人那样复杂往往使他们出来的人。虽然,我将满足您以相反的顺序问题。它流动比的方式更好。

您的问题:


  

我应该每次我开始我的应用程序时请求一个新的令牌?


排序。随着UIRemoteNotifications,你从来没有真正要求一个道理,这么多的请求许可,并获得令牌。你应该做的是落实应用程序:didRegisterForRemoteNotificationsWithDeviceToken:在你的应用程序代理。这种方法(与它的错误醒目的兄弟姐妹一起应用程序:didFailToRegisterForRemoteNotificationsWithError:)是 registerForRemoteNotificationTypes回调:。这是调用最佳实践 registerForRemoteNotificationTypes:的application:didFinishLaunchingWithOptions:。 (不要担心所有的到处乱飞的方法名。稍后我会介绍codewise)。


  

如果我没有要求用户关闭通知我的应用程序一个新的令牌,检索令牌是不同,或者是永远不变的?


也许吧。该设备令牌如有更改,出于安全原因,但一般你不应该必须的的关注它的变化。


  

是否有可能来检查,如果用户已禁用远程通知我我的应用程序的code里面的应用程序?


为什么,是的。 UIApplicationDelegate 有一个名为 enabledRemoteNotificationTypes 方法,得到所有你所需要和您的用户启用远程通知类型。马上有更多介绍。

全部放在一起:

在这一天结束时,你应该像这样结束:

 的#define deviceTokenKey @devtok
#定义remoteNotifTypes UIRemoteNotificationTypeBadge | \\
                         UIRemoteNotificationTypeAlert - (无效)的applicationDidFinishLaunching:(NSNotification *)aNotification
{
    //通用设置和诸如此类的东西    [UIApplication的sharedApplication] registerForRemoteNotificationTypes:remoteNotifTypes];
    如果(([[NSUserDefaults的standardUserDefaults] stringForKey:deviceTokenKey])及&放大器;
        ([[UIApplication的sharedApplication] enabledRemoteNotificationTypes]!= remoteNotifTypes))
    {
        //用户已禁用可能推。相应地作出反应。
    }
} - (无效)应用:(*的UIApplication)的应用didRegisterForRemoteNotificationsWithDeviceToken:(NSData的*)标记
{
    * NSString的deviceToken = [令牌描述];
    deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@< withString:@];
    deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@>中withString:@];
    deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@withString:@];    如果([[NSUserDefaults的standardUserDefaults] stringForKey:deviceTokenKey])
    {
        如果([[[NSUserDefaults的standardUserDefaults] stringForKey:deviceTokenKey] isEqualToString:deviceToken]!)
        {
            [NSUserDefaults的standardUserDefaults]的setObject:deviceToken forKey:deviceTokenKey];
            [NSUserDefaults的standardUserDefaults]同步]。            //用户允许的推动。相应地作出反应。
        }
    }
    其他
    {
        [NSUserDefaults的standardUserDefaults]的setObject:deviceToken forKey:deviceTokenKey];
        [NSUserDefaults的standardUserDefaults]同步]。        //用户允许的推动。相应地作出反应。
    }
} - (无效)应用:(*的UIApplication)的应用didFailToRegisterForRemoteNotificationsWithError:(NSError *)错误
{
    的NSLog(应用程序:%@ didFailToRegisterForRemoteNotificationsWithError:%@,应用程序,[错误localizedDescription]);
}

I'm registering my iPhone application for Remote Notification.

Here my scenario:

  1. User opens my app and register for remote notification
  2. User turns notification off for my app
  3. User open my app
  4. User turns notification on for my app

My questions are:

  1. Is it possible to check if the user has disabled remote notification for my app inside my app's code?
  2. If I request a new token without user turn off notification for my app, the retrieved token is different or is always the same?
  3. Should I request a new token every time I start my app?

解决方案

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.

Your questions:

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

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?

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.

Putting it all together:

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天全站免登陆