iOS推送(一):本地推送
来源:互联网 发布:通联支付网络这么挣钱 编辑:程序博客网 时间:2024/05/22 09:41
以iOS8.0和iOS10.0两个版本来创建一个本地推送:
- (void)localPush{// iOS8.0下本地推送的创建方式 if ([UIDevice currentDevice].systemVersion.floatValue < 10.0) { UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0]; localNotification.alertBody = @"这是一条本地推送,这是推送主体!"; localNotification.soundName = UILocalNotificationDefaultSoundName; localNotification.applicationIconBadgeNumber += 1; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; // iOS10.0 }else { // 给推送一个标识符 NSString *identifier = @"localPush.identifier"; // 推送内容 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; content.body = @"这是iOS10的本地推送,这是推送主体!"; content.badge = @1; // 推送触发器 // UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES]; NSDateComponents *components = [NSDateComponents new]; components.second = 5.0f; UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES]; // 推送请求 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; // 把推送请求添加到推送中心 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { NSLog(@"%@", [error localizedDescription]); }]; }}
推送创建完毕后,那么我们便要考虑收到推送后会执行什么样的操作,这里主要介绍处理接收到推送的地方。
首先我们要先注册推送通知。8.0和10.0注册推送还是很不一样的。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 8.0系统注册推送 if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]; [application registerUserNotificationSettings:settings]; // iOS10.0注册推送 }else{ [UNUserNotificationCenter currentNotificationCenter].delegate = self; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) { if (error != nil) { NSLog(@"%@", [error localizedDescription]); } }]; [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"%@", settings); }]; }// 如果是点击推送进入的App,那么lauchOptions会有一个UIApplicationLaunchOptionsLocalNotificationKey字段,如果这个字段不为空,代表是点击推送进入,并且可以获得推送的内容。注意这个key在iOS10以后也有值,不要和userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:这个方法重复了。所以要判断一下版本是否小于10.0。 if (([[UIDevice currentDevice].systemVersion floatValue] < 10.0) && launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) { // ......在点击推送进入App后会执行的代码。 } return YES;}
iOS10以后,如果程序被销毁后(即程序退出后),收到推送,点击推送进入app,会调用该方法;在该方法里可以执行需要执行的代码,注意两点:
一.要调用一下completionHandler()
;
二.要在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中设置一下代理:
;
[UNUserNotificationCenter currentNotificationCenter].delegate = self
否则该代理方法不会执行。
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ //......程序退出时收到推送执行的操作 completionHandler();}
iOS8.0以后,如果程序在前台,收到推送会调用该方法:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ //......程序在前台时收到推送执行的操作}
iOS10.0以后,如果程序在前台,收到推送会调用该方法:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ //......程序在前台时收到推送执行的操作 completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);}
0 0
- iOS推送(一):本地推送
- ios本地消息推送(一)
- IOS-推送消息(本地推送)
- ios推送(一)
- iOS推送:实现本地推送
- iOS推送:实现本地推送
- iOS推送:实现本地推送
- iOS推送:实现本地推送
- ios (推送)之本地推送
- iOS推送:实现本地推送
- ios (推送)之本地推送
- iOS推送之本地推送
- ios推送之本地推送
- 干货——iOS本地推送与远程推送详解(一图看懂)
- iOS本地推送与远程推送详解(一图看懂)
- iOS本地推送(本地通知)
- iOS本地推送(本地通知)
- iOS 本地推送 (本地通知)
- springmvc初始化的时候,bean都实例化了两次
- leetCode练习(11)
- 获取触点的坐标
- Sqlite(1)之分页查询
- HTML5引入的新数组TypedArray介绍
- iOS推送(一):本地推送
- iOS内存管理
- 不要写死!天猫App的动态化配置中心实践
- 2016 ACM/ICPC Asia Regional Qingdao hdu5890 Eighty seven(DP+bitset优化妙用)
- Eclipse安装Spket插件并破解
- day6_超链接
- oracle重做日志文件组v$log
- 把一个数组的值存入二叉树中,然后进行3种方式的遍历(完整代码)
- EditText被软键盘挡住一部分