ios didRegisterForRemoteNotificationsWithDeviceToken 何时被调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10983536/
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
When is didRegisterForRemoteNotificationsWithDeviceToken called?
提问by jwl
There are a lot of questions about didRegisterForRemoteNotificationsWithDeviceToken but they all sidestep a very direct question which I cannot seem to find an exact answer to.
有很多关于 didRegisterForRemoteNotificationsWithDeviceToken 的问题,但他们都回避了一个非常直接的问题,我似乎找不到确切的答案。
For an app which is properly set up for notifications in all other ways and has proper network connectivity: when is didRegisterForRemoteNotificationsWithDeviceToken called? Some possible choices might be:
对于以所有其他方式正确设置通知并具有正确网络连接的应用程序:什么时候调用 didRegisterForRemoteNotificationsWithDeviceToken?一些可能的选择可能是:
- Every time the app starts
- Only after the initial prompt to the user to accept push notifications
- Something else?
- 每次应用程序启动时
- 只有在初始提示用户接受推送通知后
- 还有什么?
采纳答案by Abd Sani Abd Jalal
The application delegate will call the method upon successful registration of remote notification after you call this method in your UIApplication:
在您的 UIApplication 中调用此方法后,应用程序委托将在成功注册远程通知后调用该方法:
(void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
(void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
According to: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html
根据:http: //developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html
When you send this message, the device initiates the registration process with Apple Push Service. If it succeeds, the application delegate receives a device token in the application:didRegisterForRemoteNotificationsWithDeviceToken:method; if registration doesn't succeed, the delegate is informed via the application:didFailToRegisterForRemoteNotificationsWithError:method. If the application delegate receives a device token, it should connect with its provider and pass it the token.
当您发送此消息时,设备会启动 Apple Push Service 的注册过程。如果成功,应用程序委托会在应用程序中收到一个设备令牌:didRegisterForRemoteNotificationsWithDeviceToken:方法;如果注册不成功,则通过application:didFailToRegisterForRemoteNotificationsWithError:方法通知委托人。如果应用程序委托收到设备令牌,它应该与其提供者连接并将令牌传递给它。
Now, to elaborate further, normally an app will call the registerForRemoteNotificationTypesin your didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsin your application delegate. And therefore, the application:didRegisterForRemoteNotificationsWithDeviceTokenis then usually called moments after the launch of the application.
现在,为了进一步阐述,通常一个应用程序将调用registerForRemoteNotificationTypes在didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions在您的应用程序委托。因此,application:didRegisterForRemoteNotificationsWithDeviceToken通常在应用程序启动后不久被调用。
Edit:The application:didRegisterForRemoteNotificationsWithDeviceToken still gets called for subsequents registration after the first.
编辑:application:didRegisterForRemoteNotificationsWithDeviceToken 在第一个之后仍然会被调用以进行后续注册。
回答by rooster117
When the app is first run it will ask the user whether they will allow remote notifications. If they say yes then it will fire didRegisterForRemoteNotificationsWithDeviceToken at that time and every time after it will fire this function when the app is first opened. If they say no then it will not be fired unless they went into settings and allowed notifications on the app.
当应用程序第一次运行时,它会询问用户是否允许远程通知。如果他们说是,那么它会在那个时候触发 didRegisterForRemoteNotificationsWithDeviceToken 并且每次在第一次打开应用程序时它都会触发这个函数。如果他们说不,那么除非他们进入设置并允许应用程序上的通知,否则它不会被触发。
回答by Denny
There can be many reasons, check some reasons
可能有很多原因,检查一些原因
If you run the app in the simulator, the application:didFailToRegisterForRemoteNotificationsWithError: method will be called as push notifications are not supported in the simulator.
Check your deice internet connection if not connect it.
如果您在模拟器中运行该应用程序,则 application:didFailToRegisterForRemoteNotificationsWithError: 方法将被调用,因为模拟器不支持推送通知。
如果没有连接,请检查您的设备互联网连接。
For more info check Link
有关更多信息,请查看链接