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

相关文章

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、额外提示总结一、下载依赖登

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me