启用 iOS 8 的设备在代码更新后未收到推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25909568/
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 8 enabled device not receiving PUSH notifications after code update
提问by Zigglzworth
I recently upgraded one of my test iphones to iOS 8 and then upgraded the PUSH registration code as below (using xCode 6)
我最近将我的一台测试 iphone 升级到 iOS 8,然后升级如下 PUSH 注册码(使用 xCode 6)
-(BOOL)hasNotificationsEnabled {
NSString *iOSversion = [[UIDevice currentDevice] systemVersion];
NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject];
float versionVal = [prefix floatValue];
if (versionVal >= 8)
{
NSLog(@"%@", [[UIApplication sharedApplication] currentUserNotificationSettings]);
//The output of this log shows that the app is registered for PUSH so should receive them
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
return YES;
}
}
else
{
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types != UIRemoteNotificationTypeNone){
return YES;
}
}
return NO;
}
-(void)registerForPUSHNotifications {
NSString *iOSversion = [[UIDevice currentDevice] systemVersion];
NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject];
float versionVal = [prefix floatValue];
if (versionVal >= 8)
{
//for iOS8
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
}
Despite this upgrade and the fact that [[UIApplication sharedApplication] currentUserNotificationSettings] shows PUSH is enabled for the device, I am not receiving PUSH notifications.
尽管进行了此升级并且 [[UIApplication sharedApplication] currentUserNotificationSettings] 显示设备已启用推送,但我没有收到推送通知。
I am using Parse and doing everything by the book as far as they are concerned ( https://parse.com/tutorials/ios-push-notifications) .
就他们而言,我正在使用 Parse 并按本书做所有事情(https://parse.com/tutorials/ios-push-notifications)。
Is anyone experiencing the same issue? Is there something else that I may be missing ?
有没有人遇到同样的问题?还有什么我可能会遗漏的吗?
回答by pankaj
The way to register for push notifications has been changed in iOS 8: Below is the code for all versions till iOS 9:
iOS 8 更改了注册推送通知的方式:以下是 iOS 9 之前所有版本的代码:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
In case you want to check whether push notifications are enabled or not use below code:
如果您想检查是否启用了推送通知,请使用以下代码:
- (BOOL) pushNotificationOnOrOff
{
if ([UIApplication instancesRespondToSelector:@selector(isRegisteredForRemoteNotifications)]) {
return ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]);
} else {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (types & UIRemoteNotificationTypeAlert);
}
}
#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
Above code will run on Xcode 6+ only...
上面的代码只能在 Xcode 6+ 上运行...
回答by Nits007ak
Add this line at the top of your .m
file
在.m
文件顶部添加这一行
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
1) You have to put condition when you register for push notification in IOS8
.
add this code in application did finish launch
.
1)您必须在IOS8
. 在 application 中添加此代码did finish launch
。
if(IS_IOS_8_OR_LATER) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
2) Then add this methods for IOS8
2)然后添加这个方法 IOS8
#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
Then you notification delegate method will be called. hope this will help you !!!
然后你的通知委托方法将被调用。希望能帮到你 !!!
回答by karim
Runtime and old compiler safe options ... if you run in old xcode (5.0 or earlier)
运行时和旧编译器安全选项...如果您在旧 xcode(5.0 或更早版本)中运行
// changes of API in iOS 8.0
- (void) registerForPushNotification
{
NSLog(@"registerForPushNotification");
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 //__IPHONE_8_0 is not defined in old xcode (==0). Then use 80000
NSLog(@"registerForPushNotification: For iOS >= 8.0");
[[UIApplication sharedApplication] registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
} else {
NSLog(@"registerForPushNotification: For iOS < 8.0");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
}
回答by Muzammil
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// For iOS 8
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
// For iOS < 8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
After this:
在这之后:
- Delete the apps who don't have Push notifications any more form the device.
- Turn the device off completely and turn it back on.
- Go to settings and set date and time ahead a day
- Turn the device off again and turn it back
- Install the apps again and It'll ask for notification like the first time you installed It.
- Reset your date / time again if not automatically set.
- 从设备中删除不再有推送通知的应用程序。
- 完全关闭设备并重新打开。
- 转到设置并提前一天设置日期和时间
- 再次关闭设备并将其重新打开
- 再次安装应用程序,它会像第一次安装它一样要求通知。
- 如果未自动设置,请再次重置您的日期/时间。
This will reset the notification settings. Re install the app and all will work fine :)
这将重置通知设置。重新安装应用程序,一切都会正常工作:)
回答by Sujith Thankachan
Try This if you have both iOS 8 and older versions:
如果您同时拥有 iOS 8 和旧版本,请尝试此操作:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// For iOS 8
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
// For iOS < 8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
From Apple's Development site:
来自 Apple 的开发网站:
UIUserNotificationSettings
A UIUserNotificationSettings object encapsulates the types of notifications that can be displayed to the user by your app. Apps that use visible or audible alerts in conjunction with a local or push notification must register the types of alerts they employ. UIKit correlates the information you provide with the user's preferences to determine what types of alerts your app is allowed to employ.
Use this class to encapsulate your initial registration request and to view the request results. After creating an instance of this class and specifying your preferred settings, call the
registerUserNotificationSettings:
method of the UIApplication class to register those settings. After checking your request against the user preferences, the app delivers the results to theapplication:didRegisterUserNotificationSettings:
method of its app delegate. The object passed to that method specifies the types of notifications that your app is allowed to use.In addition to registering your app's alert types, you can also use this class to register groups of custom actions to display in conjunction with local or push notifications. Custom actions represent immediate tasks your app can perform in response to the notification. You define groups of actions and associate the entire group with a given notification. When the corresponding alert is displayed, the system adds buttons for each action you specified. When the user taps the button for one of the actions, the system wakes your app and calls the
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
orapplication:handleActionWithIdentifier:forLocalNotification:completionHandler:
method of its app delegate. Use those methods to perform the requested action.
UI用户通知设置
UIUserNotificationSettings 对象封装了应用程序可以向用户显示的通知类型。将可见或可听警报与本地或推送通知结合使用的应用程序必须注册它们使用的警报类型。UIKit 将您提供的信息与用户的偏好相关联,以确定您的应用程序可以使用哪些类型的警报。
使用该类封装您的初始注册请求并查看请求结果。创建此类的实例并指定首选设置后,调用
registerUserNotificationSettings:
UIApplication 类的方法来注册这些设置。根据用户偏好检查您的请求后,应用程序将结果传递给application:didRegisterUserNotificationSettings:
其应用程序委托的方法。传递给该方法的对象指定允许您的应用使用的通知类型。除了注册应用程序的警报类型之外,您还可以使用此类来注册自定义操作组以与本地或推送通知一起显示。自定义操作代表您的应用可以为响应通知而立即执行的任务。您定义操作组并将整个组与给定的通知相关联。当显示相应的警报时,系统会为您指定的每个操作添加按钮。当用户为其中一项操作点击按钮时,系统会唤醒您的应用程序并调用其应用程序委托的
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
orapplication:handleActionWithIdentifier:forLocalNotification:completionHandler:
方法。使用这些方法来执行请求的操作。
回答by Divya Bharathi
Its very simple:
它非常简单:
Add following line in didFinishLaunchingWithOptions
of your project in xcode6
在didFinishLaunchingWithOptions
您的项目中添加以下行xcode6
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
回答by Adelmo Júnior
The code below resolved:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIUserNotificationType types;
types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
if (types & UIUserNotificationTypeAlert)
pushEnabled=YES;
else
pushEnabled=NO;
}
else
{
UIRemoteNotificationType types;
types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert)
pushEnabled=YES;
else
pushEnabled=NO;
}
回答by Raphael
I tried to get the type information of the settings object and found out that the types property is basically a bitmask. Here is how you can extract the informations.
尝试获取settings对象的类型信息,发现types属性基本上是位掩码。以下是提取信息的方法。
My example is in Swift and >= iOS 8.0 though.
我的例子是在 Swift 和 >= iOS 8.0 中。
let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
if settings.types.rawValue & UIUserNotificationType.Alert.rawValue == UIUserNotificationType.Alert.rawValue
{
// can receive alert!
}
else
{
// if the user is not even able to receive alerts, show him a hint on how to reenable notifications in system settings
}
if settings.types.rawValue & UIUserNotificationType.Badge.rawValue == UIUserNotificationType.Badge.rawValue
{
// can receive badge!
}
if settings.types.rawValue & UIUserNotificationType.Sound.rawValue == UIUserNotificationType.Sound.rawValue
{
// can receive sound!
}