gpt4 book ai didi

ios - registerForRemoteNotifications 方法未被正确调用

转载 作者:技术小花猫 更新时间:2023-10-29 10:06:21 29 4
gpt4 key购买 nike

我目前正在使用 Xcode6 beta(第一个版本)。使用 parse.com,我正在尝试实现推送通知。在我的应用委托(delegate)中,我有

  [application registerForRemoteNotifications];

当我在我的 ios8 beta iPhone 上运行它时,该应用程序不会询问我是否要启用推送通知,并且永远不会调用相应的 application:didRegisterForRemoteNotificationsWithDeviceToken:。但是,当我尝试在 ios7 iPhone 上运行它时,应用程序崩溃了,并且我收到了 registerForRemoteNotifications 方法的 unrecognized selector 错误。

然后,我尝试在以前版本的 Xcode(5.0 版)上运行它,但收到编译错误 no visible @interface declares registerForRemoteNotifications

我假设这个错误与过渡到 iOS 8 的错误有关,但我不确定如何解决这个问题。

最佳答案

阅读 UIApplication.h 中的代码。

您将知道如何做到这一点。

首先:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

像这样添加代码

#ifdef __IPHONE_8_0
//Right, that is the point
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif

第二个:

添加这个函数

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:@"declineAction"]){
}
else if ([identifier isEqualToString:@"answerAction"]){
}
}
#endif

你可以得到deviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

如果还是不行,使用这个函数和 NSLog error

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

关于ios - registerForRemoteNotifications 方法未被正确调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24485681/

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