gpt4 book ai didi

iOS 8 registerForRemoteNotifications 正确实现放置

转载 作者:行者123 更新时间:2023-11-28 17:52:32 27 4
gpt4 key购买 nike

我见过很多不同的通知权限请求实现,但我仍然不知道哪一个是最好的。这可能是重复的,但我想要一个包含最新 Apple 推荐的 2015 答案。

我的一般问题是,是将 registerForRemoteNotifications 调用紧接在 registerUserNotificationSettings: 调用之前,紧接在它之后,还是放在 application:didRegisterUserNotificationSettings:委托(delegate)方法。

目前,我是这样的:

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];

在设备上测试之后(这是第一次,因为用户对标准警报的响应被恼人地缓存,即使在重新安装应用程序之后也是如此),我在控制台中得到了这个:

2015-02-16 16:27:33.025 AppName[2679:591060] NOTIFICATIONS: didRegisterForRemoteNotificationsWithDeviceToken: <XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX>

然后 Apple 的标准警报出现了,在选择 之后,我在控制台中得到了这些:

2015-02-16 16:32:26.631 AppName[2679:591060] NOTIFICATIONS: application:didRegisterUserNotificationSettings:: <UIUserNotificationSettings: 0x174634300; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>
2015-02-16 16:32:27.229 AppName[2679:591060] NOTIFICATIONS: didRegisterForRemoteNotificationsWithDeviceToken: <XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX> // same token

didRegisterForRemoteNotificationsWithDeviceToken: 委托(delegate)是如何在响应警报之前被调用的?是因为这两个电话是一个接一个吗?

更新:

我做了另一个测试,在标准警报上我选择了No,我在控制台中得到了这些:

2015-02-16 16:54:37.472 AppName[2717:595759] NOTIFICATIONS: didRegisterForRemoteNotificationsWithDeviceToken: <XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX>
2015-02-16 16:54:44.353 AppName[2717:595759] NOTIFICATIONS: application:didRegisterUserNotificationSettings:: <UIUserNotificationSettings: 0x174432cc0; types: (none);>

因此 application:didFailToRegisterForRemoteNotificationsWithError: 从未被调用

这怎么可能?这与 Apple 的文档相反。

最佳答案

My general question is whether to put the registerForRemoteNotifications call immediately before the registerUserNotificationSettings: call, immediately after it, or in it's application:didRegisterUserNotificationSettings: delegate method.

你得到一个好的答案了吗?我相信正确的地方是在 application:didRegisterUserNotificationSettings 在 app Delegate 中。从上面假设你想在你的 View Controller 而不是应用程序委托(delegate)中处理所有这些,在这个响应和 registerForRemoteNotifications 的响应中使用 NSNotification 并在你的 View Controller 中放置一个相应的监听器。

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
if (notificationSettings.types != UIUserNotificationTypeNone) {
//register to receive notifications
[application registerForRemoteNotifications];
} else {
// same as response to didFailToRegisterForRemoteNotificationsWithError
NSDictionary* data = [NSDictionary dictionaryWithObject:@"" forKey:@"deviceToken"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notificationsRegistered" object:self userInfo:data];
}
}

关于iOS 8 registerForRemoteNotifications 正确实现放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28543972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com