远程通知令牌处理

发布于 2024-12-23 18:09:00 字数 311 浏览 4 评论 0原文

我正在注册我的 iPhone 应用程序以进行远程通知。

这是我的场景:

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

我的问题是:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-12-30 18:09:00

那么您想了解 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]);
}

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]);
}
诠释孤独 2024-12-30 18:09:00
  1. 我不确定你可以,我想如果用户禁用了远程通知,通知仍会发送到用户的设备,但不会显示。

2.
可以根据此 SO 更改检索到的令牌。

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

  1. I'm not sure you can, I guess if user has disabled remote notification, the notification will still be sent to user's device, but it will not be displayed.

2.
The retrieved token can be changed according to this SO.

3.
Yes, in case the token changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文