iOS:如何注册推送通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8624898/
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
iOS: how to register for push notifications?
提问by dhrm
I'm trying to implement Push Notifications for my iOS 5 application by the guide from Ray Wenderlich: http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12.
我正在尝试按照 Ray Wenderlich 的指南为我的 iOS 5 应用程序实现推送通知:http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 。
I've inserted the following into my didFinishLaunchingWithOptions
method in my AppDelegate:
我已将以下内容插入到didFinishLaunchingWithOptions
我的 AppDelegate 方法中:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
When running the application on my device (not simulator) the popup/alert telling me to accept push notifications isn't displayed. I've inserted a debug-point on the line, and I can see, that the registerForRemoteNotificationTypes
is called.
在我的设备(不是模拟器)上运行应用程序时,不会显示告诉我接受推送通知的弹出窗口/警报。我已经在线路上插入了一个调试点,我可以看到,它registerForRemoteNotificationTypes
被调用了。
Why is nothing happening?
为什么什么都没有发生?
采纳答案by YuAo
Maybe, delete your app and try again. That dialog only appears once. But I'm not sure that whether that dialog will appear again when you reinstall that app.
也许,删除您的应用程序并重试。该对话框只出现一次。但是我不确定当您重新安装该应用程序时该对话框是否会再次出现。
You can also go to your setting app the notification center, see if your app is on the list.
你也可以到你的设置应用的通知中心,看看你的应用是否在列表中。
You can also add a break point and see if didRegisterForRemoteNotificationsWithDeviceToken executes.
您还可以添加断点并查看 didRegisterForRemoteNotificationsWithDeviceToken 是否执行。
回答by Johno
I had this exact issue (with the same tutorial no less), and found that I was signing the code with the wrong provisioning profile.
我遇到了这个确切的问题(同样的教程不少),并发现我正在使用错误的配置文件签署代码。
Specifically, I only enabled "Production" push notifications for my app (since I didn't want to make certificates twice etc) but my Build Settings in Xcode used "iPhone Development" as the default "Code Signing Identity" for "Release", rather than "iPhone Distribution" as it should have been. This seemed to be the default setting in my test app.
具体来说,我只为我的应用程序启用了“生产”推送通知(因为我不想制作两次证书等),但我在 Xcode 中的构建设置使用“iPhone 开发”作为“发布”的默认“代码签名身份”,而不是应有的“iPhone 发行版”。这似乎是我的测试应用程序中的默认设置。
Hopefully I can stop someone else wasting time on the same problem.
希望我可以阻止其他人在同一问题上浪费时间。
回答by Genki
From iOS 8 there's new method for this. Straight from UIApplication.h
:
从 iOS 8 开始,有一个新方法。直接来自UIApplication.h
:
- (void)registerForRemoteNotifications NS_AVAILABLE_IOS(8_0);
Calling this will result in either application:didRegisterForRemoteNotificationsWithDeviceToken:
or application:didFailToRegisterForRemoteNotificationsWithError:
to be called on the application delegate.
调用此方法将导致应用程序委托调用application:didRegisterForRemoteNotificationsWithDeviceToken:
或application:didFailToRegisterForRemoteNotificationsWithError:
。
Note: these callbacks will be made only if the application has successfully registered for user notifications with registerUserNotificationSettings:
, or if it is enabled for Background App Refresh.
注意:仅当应用程序已成功注册用户通知时registerUserNotificationSettings:
,或启用后台应用程序刷新时,才会进行这些回调。