iOS10 添加本地推送(Local Notification)

2024-01-12 17:38

本文主要是介绍iOS10 添加本地推送(Local Notification),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


转: http://blog.csdn.net/lincsdnnet/article/details/52970747


iOS10 添加本地推送(Local Notification)

新的推送注册机制

[objc] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UserNotifications/UserNotifications.h>  
  2. #import "AppDelegate.h"  
  3. @interface AppDelegate ()<UNUserNotificationCenterDelegate>  
  4.   
  5. @end  
  6.   
  7. @implementation AppDelegate  
  8.   
  9. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  10.     // 使用 UNUserNotificationCenter 来管理通知  
  11.     UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];  
  12.     //监听回调事件  
  13.     center.delegate = self;  
  14.       
  15.     //iOS 10 使用以下方法注册,才能得到授权  
  16.     [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)  
  17.                           completionHandler:^(BOOL granted, NSError * _Nullable error) {  
  18.                               // Enable or disable features based on authorization.  
  19.                           }];  
  20.       
  21.     //获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取  
  22.     [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {  
  23.           
  24.     }];  
  25.     return YES;  
  26. }  
  27.   
  28. #pragma mark - UNUserNotificationCenterDelegate  
  29. //在展示通知前进行处理,即有机会在展示通知前再修改通知内容。  
  30. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{  
  31.     //1. 处理通知  
  32.       
  33.     //2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式  
  34.     completionHandler(UNNotificationPresentationOptionAlert);  
  35. }  
  36. @end  

推送本地通知

[objc] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //使用 UNNotification 本地通知  
  2. +(void)registerNotification:(NSInteger )alerTime{  
  3.       
  4.     // 使用 UNUserNotificationCenter 来管理通知  
  5.     UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];  
  6.       
  7.     //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。  
  8.     UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];  
  9.     content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];  
  10.     content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"  
  11.  arguments:nil];  
  12.     content.sound = [UNNotificationSound defaultSound];  
  13.       
  14.     // 在 alertTime 后推送本地推送  
  15.     UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger  
  16.  triggerWithTimeInterval:alerTime repeats:NO];  
  17.   
  18.     UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"  
  19.  content:content trigger:trigger];  
  20.       
  21.     //添加推送成功后的处理!  
  22.     [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {  
  23.         UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];  
  24.         UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];  
  25.         [alert addAction:cancelAction];  
  26.         [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];  
  27.     }];  
  28. }  

iOS 10 以前本地推送通知:

[objc] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. + (void)registerLocalNotificationInOldWay:(NSInteger)alertTime {  
  2.     // ios8后,需要添加这个注册,才能得到授权  
  3.     // if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {  
  4.     // UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;  
  5.     // UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type  
  6.     // categories:nil];  
  7.     // [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  
  8.     // // 通知重复提示的单位,可以是天、周、月  
  9.     // }  
  10.       
  11.     UILocalNotification *notification = [[UILocalNotification alloc] init];  
  12.     // 设置触发通知的时间  
  13.     NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];  
  14.     NSLog(@"fireDate=%@",fireDate);  
  15.       
  16.     notification.fireDate = fireDate;  
  17.     // 时区  
  18.     notification.timeZone = [NSTimeZone defaultTimeZone];  
  19.     // 设置重复的间隔  
  20.     notification.repeatInterval = kCFCalendarUnitSecond;  
  21.       
  22.     // 通知内容  
  23.     notification.alertBody =  @"该起床了...";  
  24.     notification.applicationIconBadgeNumber = 1;  
  25.     // 通知被触发时播放的声音  
  26.     notification.soundName = UILocalNotificationDefaultSoundName;  
  27.     // 通知参数  
  28.     NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"开始学习iOS开发了" forKey:@"key"];  
  29.     notification.userInfo = userDict;  
  30.       
  31.     // ios8后,需要添加这个注册,才能得到授权  
  32.     if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {  
  33.         UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;  
  34.         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type  
  35.                                                                                  categories:nil];  
  36.         [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  
  37.         // 通知重复提示的单位,可以是天、周、月  
  38.         notification.repeatInterval = NSCalendarUnitDay;  
  39.     } else {  
  40.         // 通知重复提示的单位,可以是天、周、月  
  41.         notification.repeatInterval = NSDayCalendarUnit;  
  42.     }  
  43.       
  44.     // 执行通知注册  
  45.     [[UIApplication sharedApplication] scheduleLocalNotification:notification];  
  46. }  


效果图






这篇关于iOS10 添加本地推送(Local Notification)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/598646

相关文章

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Java实现本地缓存的常用方案介绍

《Java实现本地缓存的常用方案介绍》本地缓存的代表技术主要有HashMap,GuavaCache,Caffeine和Encahche,这篇文章主要来和大家聊聊java利用这些技术分别实现本地缓存的方... 目录本地缓存实现方式HashMapConcurrentHashMapGuava CacheCaffe

Maven项目打包时添加本地Jar包的操作步骤

《Maven项目打包时添加本地Jar包的操作步骤》在Maven项目开发中,我们经常会遇到需要引入本地Jar包的场景,比如使用未发布到中央仓库的第三方库或者处理版本冲突的依赖项,本文将详细介绍如何通过M... 目录一、适用场景说明​二、核心操作命令​1. 命令格式解析​2. 实战案例演示​三、项目配置步骤​1

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现

python如何下载网络文件到本地指定文件夹

《python如何下载网络文件到本地指定文件夹》这篇文章主要为大家详细介绍了python如何实现下载网络文件到本地指定文件夹,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下...  在python中下载文件到本地指定文件夹可以通过以下步骤实现,使用requests库处理HTTP请求,并结合o

一文详解如何查看本地MySQL的安装路径

《一文详解如何查看本地MySQL的安装路径》本地安装MySQL对于初学者或者开发人员来说是一项基础技能,但在安装过程中可能会遇到各种问题,:本文主要介绍如何查看本地MySQL安装路径的相关资料,需... 目录1. 如何查看本地mysql的安装路径1.1. 方法1:通过查询本地服务1.2. 方法2:通过MyS

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Maven如何手动安装依赖到本地仓库

《Maven如何手动安装依赖到本地仓库》:本文主要介绍Maven如何手动安装依赖到本地仓库问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、下载依赖二、安装 JAR 文件到本地仓库三、验证安装四、在项目中使用该依赖1、注意事项2、额外提示总结一、下载依赖登