【发布时间】:2015-04-13 07:59:38
【问题描述】:
我想在每天的特定时间触发本地通知,我已经编写了这段代码 sn-p 但它没有收到通知。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// running on iOS8
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
}
else // iOS 7 or earlier
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(@"didReceiveLocalNotification----");
application.applicationIconBadgeNumber = 0;
}
//ViewController类
- (void)viewDidLoad
{
[super viewDidLoad];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setHour:7];
[dateComponents setMinute:59];
NSDate *currentDate = [NSDate date]; //2015-04-13 07:56:09 +0000
NSDate *fireDate = nil;
fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody = @"Notiication success....";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
我的日志显示didReceiveLocalNotification,但通知未出现在设备中:(
我哪里出错了,请帮忙。
提前致谢
【问题讨论】:
标签: ios iphone ipad notifications uilocalnotification