iOS9的UILocalNotification问题 [英] UILocalNotification issue with iOS9

查看:110
本文介绍了iOS9的UILocalNotification问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自iOS9起,本地通知无法正常运行. 有时用户会收到通知,有时则不会.我的通知每天重复一次. 知道是什么原因引起的问题吗?我看到了一些帖子,说明iOS9中存在一个错误,但是我不确定这就是原因.

Since iOS9, local notification aren't working properly. Sometimes users receive the notification, sometimes they just don't. My notifications are repeated daily. Any idea what might causing the issue? I saw some posts, that there's a bug in iOS9, but I'm not sure that's the reason.

这是一段代码:

    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中编写以下代码

write below code in 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中编写此代码

also write this code in 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中编写以下代码.

write below code in your ViewController, where you wants to implement your local notification.

例如:在这里我初始化UILocalNotification,它的触发日期是currentTime和5seconds间隔时间.

forExample : Here I initialise UILocalNotification, and Its Firedate is currentTime and 5seconds Interval Time.

Fire localNotification之后,它将在您的屏幕上发出警报.

After Fire localNotification it will give alert on Your Screen.

- (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];
}

这篇关于iOS9的UILocalNotification问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆