iOS9 的 UILocalNotification 问题

2024-03-29

从iOS9开始,本地通知无法正常工作。 有时用户会收到通知,有时则不会。我的通知每天都会重复。 知道什么可能导致这个问题吗?我看到一些帖子说iOS9有一个bug,但我不确定这就是原因。

这是一段代码:

    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]  

感谢您的帮助


在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;

}

也在 AppDelegate.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 中编写以下代码。

例如:这里我初始化了UILocalNotification,它的Firedate是当前时间和5秒的间隔时间。

触发 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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS9 的 UILocalNotification 问题 的相关文章

随机推荐