最近开发开发一个小App,发现其实很多关联用户操作的提醒通知并不需要依赖繁杂后台来推送提醒流程,比如有时间限制的提醒,话不多说,直接上代码;
iOS8以上需要给系统注册才能使用,API:
// Registering UIUserNotificationSettings more than once results in previous settings being overwritten.
- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0);
//注册本地通知
+ (void)registerLocalNotification:(NSInteger)alertTime String:(NSString*)string Type:(Style)type{
// alertTime:推送时间 string:推送的内容 type:推送的类型
//初始化本地通知
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 设置触发通知的时间
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
notification.fireDate = fireDate;
// 时区
notification.timeZone = [NSTimeZone defaultTimeZone];//使用本地时区
// 设置重复的间隔 通知重复提示的单位,可以是天、周、月
notification.repeatInterval = 0;//0表示不重复提醒(按需填写)
//设置推送时通知栏的显示的信息
notification.alertBody=string;
//设定通知的userInfo标识该通知类型
NSDictionary *userDict=[NSDictionary dictionaryWithObject:string forKey:@"key"];
// 设置应用程序右上角的提醒个数
// 通知被触发时播放的声音
notification.soundName = UILocalNotificationDefaultSoundName;
notification.userInfo = userDict;
// ios8后,需要添加这个注册,才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
// 执行通知注册 [[UIApplication sharedApplication] scheduleLocalNotification:notification];}
属性 :repeatInterval (表示通知重复的时间)它主要由以下取值u若设置为0表示不重复;
NSCalendarUnitEra = kCFCalendarUnitEra, //表示一个世纪
NSCalendarUnitYear = kCFCalendarUnitYear, //1年
NSCalendarUnitMonth = kCFCalendarUnitMonth, //月
NSCalendarUnitDay = kCFCalendarUnitDay, // 日
NSCalendarUnitHour = kCFCalendarUnitHour, //小时
NSCalendarUnitMinute = kCFCalendarUnitMinute, //分钟
NSCalendarUnitSecond = kCFCalendarUnitSecond, // 秒
NSCalendarUnitWeekday = kCFCalendarUnitWeekday, // 星期
通知的回调方法:
-(void)application:(UIApplication*)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification{
NSString *notMess = [notification.userInfo objectForKey:@"key"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:notMess
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
// 更新显示的徽章个数
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
// 在不需要再推送时,可以取消推送
// [NotificationLocal cancelLocalNotificationWithKey:@"key"];
}
3.取消通知
3.1 取消所有的通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];
3.2取消特定的通知
API:
- (void)cancelLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
代码:
// 取消某个本地推送通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key {
// 获取所有本地通知数组
NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
for (UILocalNotification *notification in localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if (userInfo) {
// 根据设置通知参数时指定的key来获取通知参数
NSString *info = userInfo[key];
// 如果找到需要取消的通知,则取消
if (info != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
break;
}
}
}
}