iOS的本地推送
1、先注册推送(注iOS8.0之前和iOS8.0之后注册的方法不一样)在AppDelegate.m
float version = [[[UIDevice currentDevice] systemVersion] floatValue];//获取系统版本
if (version >= 8.0) {
/*
UIUserNotificationTypeNone = 0, 不发出通知
UIUserNotificationTypeBadge = 1 << 0, 改变应用程序图标右上角的数字
UIUserNotificationTypeSound = 1 << 1, 播放音效
UIUserNotificationTypeAlert = 1 << 2, 是否运行显示横幅
*/
UIUserNotificationType notifaType = UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge;
UIUserNotificationSettings *userNotifi = [UIUserNotificationSettings settingsForTypes:notifaType categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:userNotifi];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge];
}
2、注册成功和失败
//注册通知成功
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"注册成功---》deviceToken:%@", deviceToken);
}
//注册通知失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"注册失败,无法获取设备ID, 具体错误:%@", error)
本地推送 --- App在后台,程序未被杀死,用户点击了本地通知后的操作
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// 一定要判断App是否在后台
if (application.applicationState == UIApplicationStateActive) {
NSLog(@"执行前台对应的操作");
} else if (application.applicationState == UIApplicationStateInactive) {
// 后台进入前台
NSLog(@"执行后台进入前台对应的操作");
NSLog(@"%@", notification.userInfo);
} else {
// 当前App在后台
NSLog(@"执行后台对应的操作");
// notification.applicationIconBadgeNumber++;
application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber + 1;
NSString *strValue = notification.userInfo[@"detailMess"]; // 这里的 userInfo 对应通知的设置信息 userInfo
self.myMess = [NSString stringWithFormat:@"%@(App在后台且用户点击了通知后,这里是具体的通知信息详情)",strValue];
NSNotification *notification = [NSNotification notificationWithName:@"bgckMess" object:nil userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotification:notification];
NSLog(@"App还在后台,点击了本地通知后,进入这个方法.得到的本地其他信息:%@", notification.userInfo);
}
}
4、然后是在ViewController.m中,写具体的推送
for (int i = 0; i < arr.count; i++) {
NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch|NSNumericSearch|
NSWidthInsensitiveSearch|NSForcedOrderingSearch;
NSComparator sort = ^(NSString *obj1,NSString *obj2){
NSRange range = NSMakeRange(0,obj1.length);
return [obj1 compare:obj2 options:comparisonOptions range:range];
};
NSArray *resultArray = [arr sortedArrayUsingComparator:sort];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSInteger uint = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSTimeZone *zone = [NSTimeZone systemTimeZone];//获取系统的时区
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];//设置时间格式
[formatter setTimeZone:zone];//设置时区
NSDate *date = [NSDate date];
NSString *dateString = [formatter stringFromDate:date];
NSLog(@"3.1******%@", dateString);
NSDateComponents *comps = [calendar components:uint fromDate:date];
int day = (int)[comps weekday];
int hours = (int)[comps hour];
int minute = (int)[comps minute];
int second = (int)[comps second];
UILocalNotification *localNotifa = [[UILocalNotification alloc] init];
localNotifa.alertBody = @"通知啊"; // 通知的内容
// localNotifa.soundName = @"unbelievable.caf"; //通知的声音 系统的声音
localNotifa.soundName = UILocalNotificationDefaultSoundName;//使用默认提示音
localNotifa.alertAction = @":查看"; // 锁屏的时候 相当于 滑动来::查看最新重大新闻
localNotifa.alertTitle = @"有一天";
localNotifa.applicationIconBadgeNumber ++;
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm:ss"];
localNotifa.timeZone = [NSTimeZone defaultTimeZone]; // 设置时区
NSArray *aa = [resultArray[i] componentsSeparatedByString:@":"];
int time = ([aa[0] intValue]-hours)*3600+([aa[1] intValue]-minute)*60-second;
localNotifa.fireDate = [NSDate dateWithTimeIntervalSinceNow:time];
localNotifa.repeatInterval = NSCalendarUnitDay;
localNotifa.userInfo = @{@"detailMess":[NSString stringWithFormat:@"不要忘记%@哦!", @"吃饭"]};
//设置userinfo方便撤销
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
localNotifa.userInfo = info;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotifa]; // 调度通知(启动通知)
}
说明for循环的目的是一次添加一天的多次通知
5、取消推送
第一种取消某个推送
//获取所有本地通知数组
NSArray*localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
for(UILocalNotification *notification in localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if(userInfo) {
NSLog(@"%@", userInfo);
//根据设置通知参数时指定的key来获取通知参数
NSString *infoDic = [userInfo objectForKey:@"userInfo"];
//如果找到需要取消的通知,则取消
if(infoDic !=nil) {
[UIApplication sharedApplication]cancelLocalNotification:notification];//这个是取消某一个推送
break;
}
}
第二种取消所有的本地推送
[[UIApplication sharedApplication] cancelAllLocalNotifications];//取消所有的通知
这样就完成了一次一天设置多个的推送时间,上面设置的是每天重复的推送。设置具体到哪几天推送,目前还没有研究出来,有知道的小伙伴可以告诉我下,谢谢啦!第一次写博客,文字说明很少,纯代码,写的不好多多指教!
网友评论