iOS9 的 UILocalNotification 问题
Posted
技术标签:
【中文标题】iOS9 的 UILocalNotification 问题【英文标题】:UILocalNotification issue with iOS9 【发布时间】:2015-10-02 06:52:28 【问题描述】:从 ios9 开始,本地通知无法正常工作。 有时用户会收到通知,有时他们不会。我的通知每天都会重复。 知道可能导致问题的原因吗?我看到一些帖子,说 iOS9 中存在一个错误,但我不确定是不是这个原因。
这是一段代码:
NSDate *alarmDate = [date dateByAddingTimeInterval:DEFAULT_SNOOZE_DURATION * i];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = alarmDate;
localNotif.timeZone = nil;
localNotif.alertBody = alertBody;
localNotif.hasAction = YES;
localNotif.applicationIconBadgeNumber = 1;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]
感谢您的帮助
【问题讨论】:
同样的问题,我的用户抱怨IOS9更新后错过了通知。 看起来新的“iOS优化”不允许触发后续通知。 您找到解决方案了吗?由于这个原因,我收到了应用程序拒绝。我的项目在 ios8 到 ios9 之间没有代码更改,它在 ios8 中运行。 这个问题有解决办法吗?我想一种解决方法是“手动”设置其他本地通知而不是使用repeatInterval?但是我们可以安排的不同本地通知的数量有限:-( 【参考方案1】:在 AppDelegate.m 中编写以下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Override point for customization after application launch.
[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
[application registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
UIUserNotificationTypeSound categories:nil]];
return YES;
在 AppDeleagte.m 中也写这段代码
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:notification.alertBody
delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[notificationAlert show];
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
在您想要实现本地通知的 ViewController 中编写以下代码。
forExample : 这里我初始化了 UILocalNotification,它的 Firedate 是 currentTime 和 5seconds Interval Time。
触发 localNotification 后,它会在您的屏幕上发出警报。
- (void)viewDidLoad
[super viewDidLoad];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.repeatInterval = NSDayCalendarUnit;
notification.alertBody = @"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
【讨论】:
非常感谢,我一直在尝试让通知在 iOSregisterUserNotificationSettings。 official docs 中没有提到它,该死的苹果!【参考方案2】:来自本地和远程通知编程指南:
在 iOS 8 及更高版本中,使用本地或远程通知的应用 必须注册他们打算发送的通知类型。这 系统然后赋予用户限制类型的能力 您的应用显示的通知。系统没有徽章图标, 显示警报消息,或播放警报声音(如果有) 未为您的应用启用通知类型,即使它们已启用 在通知负载中指定。
如指南所示,苹果展示的示例代码为:
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
https://***.com/a/27376475/6219830 作者:Naughty_Ottsel
这是我的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UIUserNotificationType types = UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = @"Finally got it!";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
return YES;
希望对你有所帮助!
【讨论】:
非常感谢,严。它对我有用。我们是否也可以自定义通知,例如添加操作按钮?【参考方案3】:如果你需要它用于 iOS9 但在 swift 3.1 下:
final func registerToGetLocalNotificationsFor_iOS9()
let types : UIUserNotificationType = [.sound, .alert /*, .badge*/]
let mySettings = UIUserNotificationSettings(types: types, categories: nil)
UIApplication.shared.registerUserNotificationSettings(mySettings)
在 iOS10 上已弃用。
【讨论】:
【参考方案4】:在 Appdelegate.m 中包含以下代码
if(IS_OS_8_OR_LATER)
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
【讨论】:
以上是关于iOS9 的 UILocalNotification 问题的主要内容,如果未能解决你的问题,请参考以下文章