ios registerForRemoteNotifications 方法没有被正确调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24485681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
registerForRemoteNotifications method not being called properly
提问by Mahir
I'm currently using Xcode6 beta (first version). Using parse.com, I'm trying to implement push notifications. In my app delegate, I have
我目前正在使用 Xcode6 beta(第一个版本)。使用 parse.com,我正在尝试实现推送通知。在我的应用程序委托中,我有
[application registerForRemoteNotifications];
When I run it on my ios8 beta iPhone, the app does not ask if I would like to enable push notifications, and the corresponding application:didRegisterForRemoteNotificationsWithDeviceToken:
is never called. However, when I tried running it on an ios7 iPhone, the app crashed and I received an unrecognized selector
error for the registerForRemoteNotifications
method.
当我在我的 ios8 beta iPhone 上运行它时,该应用程序不会询问我是否要启用推送通知,并且application:didRegisterForRemoteNotificationsWithDeviceToken:
永远不会调用相应的。但是,当我尝试在 ios7 iPhone 上运行它时,应用程序崩溃了,我收到了unrecognized selector
该registerForRemoteNotifications
方法的错误。
Then, I tried running it on a previous version of Xcode (version 5.0), but I received the compile error no visible @interface declares registerForRemoteNotifications
然后,我尝试在以前版本的 Xcode(5.0 版)上运行它,但收到编译错误 no visible @interface declares registerForRemoteNotifications
I'm assuming this error has to do with bugs with the transition to iOS 8, but I'm not sure how I can resolve this issue.
我假设此错误与过渡到 iOS 8 的错误有关,但我不确定如何解决此问题。
回答by Madao
Read the code in UIApplication.h.
阅读 UIApplication.h 中的代码。
You will know how to do that.
你会知道如何做到这一点。
First:
第一的:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
add Code like this
添加这样的代码
#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
Second:
第二:
Add this Function
添加此功能
#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
And your can get the deviceToken in
你可以得到 deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
if it still not work , use this function and NSLog the error
如果它仍然不起作用,请使用此功能并 NSLog错误
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
回答by ingconti
A couple of observations:
几个观察:
- the question was using REMOTE, not local
- some questions deal with local, that is a completely different thing
- I agree that answer time can be very long, but you must implement call backs (aka delegate methods), as other people pointed out
- do NOT test against compiler flag, is conceptually and practically wrong
test presence of selectors as in this code:
func registerForPushForiOS7AndAbove(){ UIApplication.sharedApplication() let application = UIApplication.sharedApplication() if application.respondsToSelector(Selector("registerUserNotificationSettings:")) { let notifSettings = UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil) application.registerUserNotificationSettings(notifSettings) application.registerForRemoteNotifications() } else{ application.registerForRemoteNotificationTypes( .Sound | .Alert | .Badge ) }
}
- 问题是使用远程,而不是本地
- 有些问题是针对本地的,那是完全不同的事情
- 我同意回答时间可能很长,但正如其他人指出的那样,您必须实现回调(又名委托方法)
- 不要针对编译器标志进行测试,这在概念上和实际上都是错误的
在此代码中测试选择器的存在:
func registerForPushForiOS7AndAbove(){ UIApplication.sharedApplication() let application = UIApplication.sharedApplication() if application.respondsToSelector(Selector("registerUserNotificationSettings:")) { let notifSettings = UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil) application.registerUserNotificationSettings(notifSettings) application.registerForRemoteNotifications() } else{ application.registerForRemoteNotificationTypes( .Sound | .Alert | .Badge ) }
}
(don't miss UIBackgroundModes in PList.. can be done in Capabilities)
(不要错过 PList 中的 UIBackgroundModes .. 可以在 Capabilities 中完成)
回答by Lucien
In iOS 8 the system won't ask the user to allow your app to send Push (Remote) Notifications, it's allowed by default.
在 iOS 8 中,系统不会要求用户允许您的应用发送推送(远程)通知,默认情况下是允许的。
The user may opt-out from allowing Notifications from your app on Settings > Yor App > Notifications > Allow Notifications.
用户可以在Settings > Yor App > Notifications > Allow Notifications上选择不允许来自您的应用程序的通知。
回答by Kai Burghardt
Just use this snippet of code to register for notifications in iOS 8:
只需使用这段代码在 iOS 8 中注册通知:
UIUserNotificationSettings *settings =
[UIUserNotificationSettings
settingsForTypes: (UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
Use UIUserNotificationType
instead of UIRemoteNotificationType
!
使用UIUserNotificationType
代替UIRemoteNotificationType
!
回答by Yatheesha B L
registerForRemoteNotifications
method is available from iOS8.
registerForRemoteNotifications
方法可从 iOS8 获得。
application:didRegisterForRemoteNotificationsWithDeviceToken:
delegate method is called only when registration is success , if any failure occurs it will call application:didFailToRegisterForRemoteNotificationsWithError:
method.
application:didRegisterForRemoteNotificationsWithDeviceToken:
仅在注册成功时调用委托方法,如果发生任何失败将调用 application:didFailToRegisterForRemoteNotificationsWithError:
方法。
For more information go through : UIApplication Class reference
有关更多信息,请访问:UIApplication 类参考
Also check notification is enable for the app Settings -> Notifications -> YourApp -> Enable_Notifications
还检查通知是否为应用程序启用 Settings -> Notifications -> YourApp -> Enable_Notifications
According to the documentation, neither of the callbacks will happen until the device has a persistent connection with the push server. So if there is no wifi or data connection available, the callbacks won't happen - and apple doesn't regard this as an error condition. only errors that can happen to cause the didFail... callback are an incorrect certificate/app permissions issue (a development problem), or the user declined permission.
根据文档,在设备与推送服务器建立持久连接之前,不会发生任何回调。因此,如果没有可用的 wifi 或数据连接,回调将不会发生 - Apple 不会将此视为错误条件。只有可能导致 didFail... 回调的错误是不正确的证书/应用程序权限问题(开发问题),或者用户拒绝了权限。
Please go through Push notification programming guide
请阅读推送通知编程指南