IOS开发-本地通知

时间:2022-05-25 16:38:16
 //    注册 发送通知的方法
-(void)pushNotfation{ //--------------初始化本地通知 alloc init 虽然是UI控件 但继承NSObject
UILocalNotification *not = [[UILocalNotification alloc]init];
// 设置本地通知启动的时间
not.fireDate = [NSDate dateWithTimeIntervalSinceNow:];
// 设置通知的标题
not.alertTitle = @"开始工作了。。。";
// 设置通知的内容
not.alertBody = @"起床做码农了,,,,hahhhhh";
// 通过通知 传递 内容--传值
not.userInfo = @{@"key":@"Value"};
// 设置App图标上面红点的 显示的数字
not.applicationIconBadgeNumber = ;
//*******************************************************************
// 设置重复发送通知的时间间隔
/*
NSCalendarUnitEra = kCFCalendarUnitEra,一个世纪
NSCalendarUnitYear = kCFCalendarUnitYear, 一年
NSCalendarUnitMonth = kCFCalendarUnitMonth, 一个月
NSCalendarUnitDay = kCFCalendarUnitDay, 天
NSCalendarUnitHour = kCFCalendarUnitHour, 时
NSCalendarUnitMinute = kCFCalendarUnitMinute,分
NSCalendarUnitSecond = kCFCalendarUnitSecond,秒
NSCalendarUnitWeekday = kCFCalendarUnitWeekday, 一个礼拜
NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal,
*/
// not.repeatCalendar = kCFCalendarUnitEra;
//******************************************************************* //——----------------注册通知
//******
// UIUserNotificationTypeNone
// UIUserNotificationTypeBadge
// UIUserNotificationTypeSound
// UIUserNotificationTypeAlert 振动
//******
// respondsToSelector 方法是否可以响应
if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// 解决
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert categories:nil]]; }
// 设置声音
not.soundName = UILocalNotificationDefaultSoundName; // 发送通知
[[UIApplication sharedApplication]scheduleLocalNotification:not]; } //本地移除
-(void)removeLocalPushNotification:(UIButton*)sender
{
NSLog(@"%s",__FUNCTION__);
UIApplication* app=[UIApplication sharedApplication];
//获取当前应用所有的通知
NSArray* localNotifications=[app scheduledLocalNotifications]; if (localNotifications) { for (UILocalNotification* notification in localNotifications) { NSDictionary* dic=notification.userInfo; if (dic) {
NSString* key=[dic objectForKey:@"key"];
if ([key isEqualToString:@"name"]) {
//取消推送 (指定一个取消)
[app cancelLocalNotification:notification]; break;
}
} }
}
//取消当前应用所有的推送
//[app cancelAllLocalNotifications]; }

下一篇博客将会详细讲述--远程通知