【问题标题】:Remote Notification Token handling远程通知令牌处理
【发布时间】:2011-12-29 20:21:49
【问题描述】:

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

这是我的场景:

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

我的问题是:

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

【问题讨论】:

    标签: iphone objective-c notifications push-notification apple-push-notifications


    【解决方案1】:

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

    您的问题:

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

    有点。使用 UIRemoteNotifications,您永远不会真正请求令牌,就像请求许可和接收令牌一样。你应该做的是在你的应用程序委托中实现application:didRegisterForRemoteNotificationsWithDeviceToken:。这个方法(连同它的错误捕获兄弟application:didFailToRegisterForRemoteNotificationsWithError:)是registerForRemoteNotificationTypes: 的回调。最佳做法是在application:didFinishLaunchingWithOptions: 期间致电registerForRemoteNotificationTypes:。 (不用担心所有的方法名称都飞来飞去。我将很快解释代码)。

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

    也许吧。出于安全原因,设备令牌可能会更改,但通常您不必担心它的更改。

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

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

    把它们放在一起:

    在一天结束的时候,你应该得到这样的结果:

    #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]);
    }
    

    【讨论】:

      【解决方案2】:
      1. 我不确定你可以,我猜如果用户禁用了远程通知,通知仍然会发送到用户的设备,但不会显示。

      2。 检索到的token可以根据这个SO进行更改。

      3。 是的,以防令牌发生变化。

      【讨论】:

        猜你喜欢
        • 2012-12-12
        • 2014-12-02
        • 2017-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多