#iOS本地推送
##基本实现
//1. 创建本地通知对象
UILocalNotification *localNotifi = [UILocalNotification new];
//2. 设置属性
//2.1 设置触发时间
localNotifi.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
//2.2 设置提示内容
localNotifi.alertBody = @"今天不适合敲代码";
//2.3 设置声音 (只有真机有效)
localNotifi.soundName = UILocalNotificationDefaultSoundName;
localNotifi.applicationIconBadgeNumber = 5;
//2.4 设置 默认YES
localNotifi.hasAction = NO;
//2.5 设置 提醒样式的按钮文字 / 锁屏界面底部的文字
localNotifi.alertAction = @"回复呵呵";
//3. 调度通知 , 如果是IOS7 , 这一步写完就OK
//schedule : 调度
// 将通知加入到本地调度池中
[[UIApplication sharedApplication] scheduleLocalNotification:localNotifi];
//4. iOS8 需要增加一个方法 --> 需要授权
/**
UIUserNotificationTypeNone = 0,
UIUserNotificationTypeBadge = 1 << 0, //图标标记
UIUserNotificationTypeSound = 1 << 1, //声音
UIUserNotificationTypeAlert = 1 << 2, //提醒
*/
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
##通知调用方法
通知跳转会调用这个方法(前提是这个应用还在前台或者是在后台但是没有杀死程序),如果程序已经被杀死了就需要调用- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法.这时候的信息是存在launchOptions里面的.
//
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//这里可以填写一些页面跳转或者一些处理一些问题
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1. 判断是否有通知需要处理
// if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
//
// //2. 获取本地通知
// UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
//
// //3. 实现界面跳转
// [self switchTabBarWithNotification:notification];
//
// }
##删除通知
//5. 删除通知
// 删除当前程序注册的所有通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// 删除指定的通知 --> 一般用于干掉会重复的通知 / 或者还没有被调用的通知
//[[UIApplication sharedApplication] cancelLocalNotification:localNotifi];
// 获取通知 --> 配合删除用的
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *local in localNotifications) {
NSLog(@"local: %@", local);
}
// 如果是当前程序内收到了通知, 那么界面没有任何变化
##不常用属性
不常用属性
//2.6 设置重复 最小单位是分钟 如果此属性设置了, 那么调度池不会用完释放
//localNotifi.repeatInterval = NSCalendarUnitMinute;
//2.7 设置重复所依赖的日历 不设置的话, 默认是跟随系统设置走得
// 默认就是跟随系统走
//localNotifi.repeatCalendar = [NSCalendar calendarWithIdentifier:@"跳进官方文档, 里面就有一堆的标示符, 找chinese的选项, 就代表是农历"];
##设置通知分类
设置分类的作用是在通知下拉时,有两个按钮.这种按钮有两种模式,一种是前台,一种是后台.前台是点击会跳转到指定的app,后台不会.需要在需要在设置推送时设置分类的ID这这个ID要和NSSet *categorySet = [NSSet setWithObjects:category, nil];中的一个分类中的ID对应.
//2.6 在设置推送消息时设置分类
notifi.category = @“category”;
// 设置分类
UIMutableUserNotificationCategory *category = [UIMutableUserNotificationCategory new];
category.identifier = @"category";
// 设置一个前台按钮
UIMutableUserNotificationAction *action1 = [UIMutableUserNotificationAction new];
action1.identifier = @"foreground";
action1.activationMode = UIUserNotificationActivationModeForeground;
action1.title = @"前台";
// 设置一个后台按钮
UIMutableUserNotificationAction *action2 = [UIMutableUserNotificationAction new];
action2.identifier = @"background";
action2.activationMode = UIUserNotificationActivationModeBackground;
action2.title = @"后台";
// 设置通知出现的下拉处理按钮
[category setActions:@[action1, action2] forContext:UIUserNotificationActionContextDefault];
NSSet *categorySet = [NSSet setWithObjects:category, nil];
// 设置需要请求的权限
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:categorySet];
// 注册用户权限设置
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
//按钮点击时调用的方法
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
if ([identifier isEqualToString:@"foreground"]) {
NSLog(@"前台运行中");
} else if ([identifier isEqualToString:@"background"]) {
NSLog(@"后台运行中");
}
completionHandler();
}