iOS的本地推送UILocalNotification的使用
UILocalNotification
1 第一步:接收本地推送 2 3 实现代理方法didReceiveLocalNotification 4 5 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{ 6 7 //在此时设置解析notification,并展示提示视图 8 9 } 10 11 第二步:创建本地推送 12 - (void)createLocalNotification { 13 14 // 创建一个本地推送 15 16 17 UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; 18 19 20 21 //设置10秒之后 22 23 24 25 NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10]; 26 27 if (notification != nil) { 28 29 // 设置推送时间 30 31 notification.fireDate = pushDate; 32 33 // 设置时区 34 35 notification.timeZone = [NSTimeZone defaultTimeZone]; 36 37 // 设置重复间隔 38 39 notification.repeatInterval = kCFCalendarUnitDay; 40 // 推送声音 41 notification.soundName = UILocalNotificationDefaultSoundName; 42 // 推送内容 43 notification.alertBody = @"推送内容"; 44 //显示在icon上的红色圈中的数子 45 notification.applicationIconBadgeNumber = 1; 46 //设置userinfo 方便在之后需要撤销的时候使用 47 48 NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"]; 49 50 notification.userInfo = info; 51 52 //添加推送到UIApplication 53 54 UIApplication *app = [UIApplication sharedApplication]; 55 56 [app scheduleLocalNotification:notification]; 57 58 } 59 60 } 61 62 第三步:解除本地推送 63 64 - (void) removeLocalNotication { 65 66 // 获得 UIApplication 67 UIApplication *app = [UIApplication sharedApplication]; 68 69 //获取本地推送数组 70 NSArray *localArray = [app scheduledLocalNotifications]; 71 72 73 74 //声明本地通知对象 75 UILocalNotification *localNotification; 76 77 if (localArray) { 78 79 for (UILocalNotification *noti in localArray) { 80 81 NSDictionary *dict = noti.userInfo; 82 83 if (dict) { 84 85 NSString *inKey = [dict objectForKey:@"key"]; 86 87 if ([inKey isEqualToString:@"对应的key值"]) { 88 89 if (localNotification){ 90 91 [localNotification release]; 92 93 localNotification = nil; 94 } 95 96 localNotification = [noti retain]; 97 98 break; 99 100 } 101 102 } 103 104 } 105 106 107 //判断是否找到已经存在的相同key的推送 108 if (!localNotification) { 109 110 //不存在初始化 111 localNotification = [[UILocalNotification alloc] init]; 112 113 } 114 if (localNotification) { 115 //不推送 取消推送 116 [app cancelLocalNotification:localNotification]; 117 [localNotification release]; 118 119 return; 120 } 121 122 } 123 124 125 }
感谢您的访问!
若对您有帮助或有兴趣请关注博客:http://www.cnblogs.com/Rong-Shengcom/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?