参数
时间相关参数
(A组) fireDate 和 timeZone
- fireDate : 推送时间
- timeZone :推送时间对应的时区
设置好以后,当用户跨时区移动时,时间也会对应修改(+/-)。
(B组)repeatInterval和repeatCalendar
- repeatInterval:重复激发推送的周期(周/年/日等)
- repeatCalendar:重复激发推送的日历根据(日历根据)
如果同时设置A组和B组的话,可能会导致未知的错误。
地点相关参数
- region:区域范围,由此可设置进入某区域时进行推送
- regionTriggersOnce: BOOL类型,置为YES则每个预设区域的推送只会发送一次
- alertBody(推送显示的字符串
- alertAction(推送按钮字符
- hasAction(是否显示按钮
- alertLaunchImage(推送图标
以上是当应用不在运行时,系统处理的参数。
推送的界面和控件是固定死的,开发者没有权限自定义
其他参数:
- applicationIconBadgeNumber :强迫症最怕的App右上角红点计数
关键代码
-
1.适配iOS8以上的推送授权配置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){ [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; } else { [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge]; } }
请求用户授权发送本地推送
-
2 设置一个推送并注册到系统
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30]; //30秒后推送 localNotification.timeZone = [NSTimeZone localTimeZone]; localNotification.userInfo = @{ @"name":@"the Name", @"id":@"0", }; localNotification.alertBody = @"alertBody"; localNotification.alertTitle = @"title"; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //注入系统
-
3 移除本地推送设置
NSArray* notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; for (UILocalNotification* nof in notifArray) { if ([[nof.userInfo valueForKey:@"name"] isEqualToString:@"the Name"]) {//通过userInfo的数据来甄别相应的LocalNotification [[UIApplication sharedApplication] cancelLocalNotification:nof]; }