思路介绍
主要考虑以下点
- 如何设置推送(包括推送内容、推送时间、推送周期等)
- 如何接收推送 考虑3种情况
- app在前台运行
- app未关闭,在后台运行
- app关闭
- 接收到推送后如何响应(主要的用户交互在这里)
- 如何移除本地推送
代码说明
注册本地通知
/*!
* 注册本地通知
*/
+ (void)registerLocalNotification{
//初始化本地通知
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
//触发通知时间
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:6]; //6s后触发通知
//时区
localNotification.timeZone = [NSTimeZone defaultTimeZone];
//触发通知后弹出警告框中显示的内容
localNotification.alertBody = @"这是一个本地通知";
//触发通知时的声音(这里使用系统的声音)
localNotification.soundName = UILocalNotificationDefaultSoundName;
//设置在app icon右上角显示的未读标识
NSInteger badgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber;
localNotification.applicationIconBadgeNumber = badgeNumber+1;
//设置通知的用户信息(可以用于移除通知,也可以传其他的值到获取通知的方法中)
localNotification.userInfo = @{@"key":@"notification1"};
//ios8后,需要先注册通知类型,才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
//触发通知的频率(是一个枚举变量,可以设置每分、每时、每天等)
localNotification.repeatInterval = NSCalendarUnitDay;
} else {
//触发通知的频率(是一个枚举变量,可以设置每分、每时、每天等)
localNotification.repeatInterval = NSDayCalendarUnit;
}
//执行通知计划
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
不同情况下本地推送的接收及响应
/*!
* 接收到本地通知时的回调(有两种情况会在这里接收到通知推送。一、app在前台运行;二、app在后台运行,用户点击通知栏通知切回前台。注意:如果app在后台运行,用户接收到推送的通知,不是点击通知,而是点击app图标切回到app,是不会触发这个回调的)
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(@"----------------%@",notification);
//接收到通知后的交互
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
//更新未读标识
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
NSLog(@"%zi",badge);
badge--;
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
//根据需要移除通知
[ViewController removeLocalNotificationForName:@"notification1"];
// do something else
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//当app在关闭情况下接收到本地推送通知时,点击通知栏对应的通知启动app会调用此方法,此时通过langchOptions参数可以获取到记载了本地通知的相关信息。如果是通过点击app图标启动app,也会调用这个方法,但是launchOptions通过UIApplicationLaunchOptionsLocalNotificationKey关键字获取到的本地通知信息为空。
UILocalNotification *localNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
//获取本地通知详细信息
NSDictionary *userInfo = localNotification.userInfo;
//通过信息判断为哪条本地通知, 做出相应回应
NSString *key = [userInfo objectForKey:@"key"];
if ([key isEqualToString:@"notification1"]) {
//更新未读标识
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= 0 ? badge : 0;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
//接收到通知后的交互
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Mark" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
//根据需要移除通知
[ViewController removeLocalNotificationForName:@"notification1"];
// do something else
}
}
return YES;
}
移除本地推送
/*!
* 移除本地通知
* [[UIApplication sharedApplication] cancelAllLocalNotifications]; 可以移除所有本地推送
*
* @param notificationName 本地通知自定义的名字标识
*/
+ (void)removeLocalNotificationForName:(NSString *)notificationName{
if (!notificationName) {
return;
}
//获取所有本地通知
NSArray *arrayNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
//遍历所有通知 根据名字标识移除指定通知
for (UILocalNotification *localNotification in arrayNotifications) {
NSDictionary *dic = localNotification.userInfo;
if ([[dic objectForKey:@"key"] isEqualToString:notificationName]) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
NSLog(@"通知 %@ 已移除",notificationName);
}
}
}
参考文档:
ios推送:本地通知UILocalNotification
本地推送通知UILocalNotification
iOS本地推送(本地通知)