1、UILocalNotification相关参数ios
(1)fireDate,执行本地通知的时间;
app
(2)soundName,通知声音的名称;iphone
(3)repeatInterval,重复的周期(如kCFCalendarUnitDay,天天通知一次);函数
(4)timeZone,当地时区;测试
(5)applicationIconBadgeNumber,app icon上得数字提示;spa
(6)alertBody,弹出的通知内容;调试
(7)alertAction,点击动做;code
2、建立UILocalNotificationorm
(1)建立一个本地通知,10秒后弹出通知。ip
//进入后台,建立一个本地通知 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification) { notification.repeatInterval = kCFCalendarUnitDay; notification.timeZone = [NSTimeZone defaultTimeZone]; NSDate *currentDate = [NSDate date]; NSDate *sinceNow = [currentDate dateByAddingTimeInterval:10]; notification.fireDate = sinceNow; notification.applicationIconBadgeNumber = 1; notification.soundName = UILocalNotificationDefaultSoundName; notification.alertBody = @"测试通知"; notification.alertAction = @"打开"; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
(2)在进入前台页面时- (void)applicationDidBecomeActive:,设置appicon的applicationIconBadgeNumber的值为0;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
3、IOS8(以上)请求受权
缘由是由于在ios8中,设置应用的application badge value须要获得用户的许可。使用以下方法咨询用户是否许可应用设置application badge value,在application: didFinishLaunchingWithOptions:中加入如下代码。
//IOS8以上的系统 float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; if (systemVersion >= 8.0) { if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *noteSetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:noteSetting]; } }
备注:在IOS8 -- iphone6调试报错:
2015-12-14 16:40:50.997 Mannyi1[3932:1764085] Attempting to badge the application icon but haven't received permission from the user to badge the application
缘由是:没有用户权限,没法设置badge的值,奇怪的是权限已设置(待研究)。
4、其余
触发通知有两种状况:一种是程序没有运行,另外一种是程序已经运行;已经在运行又分为两种状况:后台运行触发和前台运行触发。
(1)我先说明一下程序没有运行的状况,能够在application: didFinishLaunchingWithOptions:函数中判断是否存在UIApplicationLaunchOptionsLocalNotificationKey。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *loc = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if(loc!=nil){
NSDictionary *dic= loc.userInfo//就使本地通知中传递过的NSDictionary,咱们能够获取其中的参数。
}
}
(2)程序已运行,在后台运行通知的时间到了,不会主动触发- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification函数,须要用户点击才会触发;程序在前台运行,会出现这样的一种状况就是:通知时间到了会触发函数,用户在通知之中心点击通知也会触发函数。因此须要区别这两种状况,用到的方法就是:当前时间与触发时间比较在很小的范围内则表示时间到了触发,反之表示在用户在通知中心点击通知触发例如:
if([[formatter dateFromString:[formatter stringFromDate:[NSDate date]]] timeIntervalSinceDate:notification.fireDate] > 0.5) {
//用户点击了,
}else{
//时间到了触发的
}
如下的例子是判断激活状态的,application: didReceiveLocalNotification函数;
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { //if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) { //判断应用程序当前的运行状态,若是是激活状态,则进行提醒,不然不提醒 if (application.applicationState == UIApplicationStateActive) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:notification.alertAction, nil]; [alert show]; } //} }