Swift - 本地消息的推送通知(附样例)
2024-08-24 21:45:49
使用UILocalNotification可以很方便的实现消息的推送功能。我们可以设置这个消息的推送时间,推送内容等。
当推送时间一到,不管用户在桌面还是其他应用中,屏幕上方会都显示出推送消息。
1,推送消息的发送
--- AppDelegate.swift ---
import UIKit @UIApplicationMain class AppDelegate : UIResponder , UIApplicationDelegate { var window: UIWindow ? func application(application: UIApplication , didFinishLaunchingWithOptions NSObject : AnyObject ]?) Bool { //开启通知 let settings UIUserNotificationSettings (forTypes: Alert , Badge , Sound ], categories: nil ) application.registerUserNotificationSettings(settings) return true } func applicationWillResignActive(application: UIApplication ) } func applicationDidEnterBackground(application: UIApplication ) } func applicationWillEnterForeground(application: UIApplication ) } func applicationDidBecomeActive(application: UIApplication ) } func applicationWillTerminate(application: UIApplication ) } } |
--- ViewController.swift ---
import UIKit class ViewController : UIViewController { override func viewDidLoad() super .viewDidLoad() //发送通知消息 scheduleNotification(12345); //清除所有本地推送 //UIApplication.sharedApplication().cancelAllLocalNotifications() } //发送通知消息 func scheduleNotification(itemID: Int ){ //如果已存在该通知消息,则先取消 cancelNotification(itemID) //创建UILocalNotification来进行本地消息通知 let localNotification UILocalNotification () //推送时间(设置为30秒以后) localNotification.fireDate NSDate (timeIntervalSinceNow: //时区 localNotification.timeZone NSTimeZone .defaultTimeZone() //推送内容 localNotification.alertBody "来自hangge.com的本地消息" //声音 localNotification.soundName UILocalNotificationDefaultSoundName //额外信息 localNotification.userInfo "ItemID" :itemID] UIApplication .sharedApplication().scheduleLocalNotification(localNotification) } //取消通知消息 func cancelNotification(itemID: Int ){ //通过itemID获取已有的消息推送,然后删除掉,以便重新判断 let existingNotification self .notificationForThisItem(itemID) as UILocalNotification ? if existingNotification nil { //如果existingNotification不为nil,就取消消息推送 UIApplication .sharedApplication().cancelLocalNotification(existingNotification!) } } //通过遍历所有消息推送,通过itemid的对比,返回UIlocalNotification func notificationForThisItem(itemID: Int )-> UILocalNotification ? let allNotifications UIApplication .sharedApplication().scheduledLocalNotifications for notification in allNotifications! let info as ! String : Int ] let number "ItemID" ] if number nil && return notification as UILocalNotification } } return nil } override func didReceiveMemoryWarning() super .didReceiveMemoryWarning() } } |
2,点击推送消息的响应
收到推送,如果点击推送内容,则会重新进入到App,这个时候会调用AppDelegate中的func
application(application: UIApplication, didReceiveLocalNotification
notification: UILocalNotification)代理方法。
application(application: UIApplication, didReceiveLocalNotification
notification: UILocalNotification)代理方法。
在这个方法中我们可以根据推送的消息内容实现相关的功能。
func application(application: UIApplication , didReceiveLocalNotification UILocalNotification ) //设定Badge数目 UIApplication .sharedApplication().applicationIconBadgeNumber let info as ! String : Int ] let number "ItemID" ] let alertController UIAlertController (title: "本地通知" , message: "消息内容:\(notification.alertBody)用户数据:\(number)" , preferredStyle: UIAlertControllerStyle . Alert )
alertController.addAction(cancel);
self .window?.rootViewController!.presentViewController(alertController, animated: true , nil ) } |
最新文章
- 如何解决Maven和SBT下载Jar包太慢
- SQL时间相关
- 程序中保存状态的方式之ViewState
- LeetCode —— Invert Binary Tree
- 【matlab】libsvm-3.18安装与使用
- thinkphp 的create()非法数据解决办法
- 分析Hibernate的事务处理机制
- 上次的AJAX定时刷新多ID不正确,这次请教了高手之后补全
- hw-text1
- 如何使用WCF调试器WcfTestClient.exe
- Android IntentService 与Alarm开启任务关闭任务
- Python之路day4
- 开源 免费 java CMS - FreeCMS1.9 移动APP管理 执行配置
- java 网络编程之TCP通信和简单的文件上传功能
- 我的前端故事----来聊聊怎么写react-native上的样式吧
- [SDOI2009]虔诚的墓主人
- [其它]iOS 12.2支持电信VoLTE了,中国电信教你如何开通:只要三步
- HDU4341-Gold miner-分组DP
- Selenium·自动化基础
- Hive 创建表
热门文章
- CreateThread函数&;&;CString::GetBuffer函数
- Scrum概述
- 60个响应式的Web设计教程–能够手机访问!
- C++的引用类型的变量到底占不占用内存空间?
- spark的环境安装
- Maven异常: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK解决(能力工场小马哥)
- python异常以及面向对象编程
- 应用删除后 Launchpad 上仍有应用图标无法删除的解决方法
- 3D游戏调研
- JavaScript 继承方式详解