IOS 本地通知

时间:2022-08-02 14:31:02

操作流程

1.接收通知

2.注册发送通知

用途:提示时间,闹钟

//接收本地通知(在Appdelegate里面实现)

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

//接收到通知之后的操作

UIAlertView *aler = [[UIAlertView alloc]initWithTitle:notification.alertTitle message:notification.alertBody delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

[aler show];

}

注册,发送通知的方法

-(void)pushNotfation{

//初始本地通知的方法

UILocalNotification *not =[[UILocalNotification alloc]init];

not.fireDate =[NSDate dateWithTimeIntervalSinceNow:10];

//    设置通知的标题

not.alertTitle = @"时间到";

//    设置通知的内容

not.alertBody = @"起床敲代码";

//    通过通知 传递 内容

not.userInfo = @{@"key":@"value"};

//    设置App图标上面红点显示的数字

not.applicationIconBadgeNumber = 1;

//    发送的间隔

not.repeatInterval =kCFCalendarUnitMonth;

/*

NSCalendarUnitEra                = kCFCalendarUnitEra,一个世纪

NSCalendarUnitYear               = kCFCalendarUnitYear, 一年

NSCalendarUnitMonth              = kCFCalendarUnitMonth, 一个月

NSCalendarUnitDay                = kCFCalendarUnitDay, 天

NSCalendarUnitHour               = kCFCalendarUnitHour, 时

NSCalendarUnitMinute             = kCFCalendarUnitMinute,分

NSCalendarUnitSecond             = kCFCalendarUnitSecond,秒

NSCalendarUnitWeekday            = kCFCalendarUnitWeekday, 一个礼拜

NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,

*/

//    注册通知

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {     [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert categories:nil]];

}

not.soundName= UILocalNotificationDefaultSoundName;

//    发送通知

[[UIApplication sharedApplication]scheduleLocalNotification:not];

//    UIUserNotificationTypeBadge| 圆圈内提示的数字

//    UIUserNotificationTypeSound| 通知提示的声音

//    UIUserNotificationTypeNone|

//    UIUserNotificationTypeAlert  振动

}