Swift - 本地消息的推送通知(附样例)
作者:hangge | 2015-05-14 11:24
注意:UILocalNotification现在已经被废弃。推送功能改用UserNotifications框架实现。关于UserNotifications框架的具体用法可以参考我的另一篇文章:Swift - UserNotifications框架使用详解2(发送本地通知)
使用UILocalNotification可以很方便的实现消息的推送功能。我们可以设置这个消息的推送时间,推送内容等。
1,推送消息的发送

--- AppDelegate.swift ---
--- ViewController.swift ---
2,点击推送消息的响应
使用UILocalNotification可以很方便的实现消息的推送功能。我们可以设置这个消息的推送时间,推送内容等。
当推送时间一到,不管用户在桌面还是其他应用中,屏幕上方会都显示出推送消息。
下面通过一个样例演示如何实现本地的消息推送。

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//开启通知
let settings = UIUserNotificationSettings(types: [.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(itemID: 12345)
//清除所有本地推送
//UIApplication.shared.cancelAllLocalNotifications()
}
//发送通知消息
func scheduleNotification(itemID:Int){
//如果已存在该通知消息,则先取消
cancelNotification(itemID: itemID)
//创建UILocalNotification来进行本地消息通知
let localNotification = UILocalNotification()
//推送时间(设置为30秒以后)
localNotification.fireDate = Date(timeIntervalSinceNow: 30)
//时区
localNotification.timeZone = NSTimeZone.default
//推送内容
localNotification.alertBody = "来自hangge.com的本地消息"
//声音
localNotification.soundName = UILocalNotificationDefaultSoundName
//额外信息
localNotification.userInfo = ["ItemID":itemID]
UIApplication.shared.scheduleLocalNotification(localNotification)
}
//取消通知消息
func cancelNotification(itemID:Int){
//通过itemID获取已有的消息推送,然后删除掉,以便重新判断
let existingNotification = self.notificationForThisItem(itemID: itemID)
if existingNotification != nil {
//如果existingNotification不为nil,就取消消息推送
UIApplication.shared.cancelLocalNotification(existingNotification!)
}
}
//通过遍历所有消息推送,通过itemid的对比,返回UIlocalNotification
func notificationForThisItem(itemID:Int)-> UILocalNotification? {
let allNotifications = UIApplication.shared.scheduledLocalNotifications
for notification in allNotifications! {
let info = notification.userInfo as! [String:Int]
let number = info["ItemID"]
if number != nil && number == itemID {
return notification as UILocalNotification
}
}
return nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2,点击推送消息的响应
收到推送,如果点击推送内容,则会重新进入到App,这个时候会调用 AppDelegate 中的 func application(_ application: UIApplication, didReceive notification: UILocalNotification) 代理方法。
在这个方法中我们可以根据推送的消息内容实现相关的功能。

func application(_ application: UIApplication,
didReceive notification: UILocalNotification) {
//设定Badge数目
UIApplication.shared.applicationIconBadgeNumber = 0
let info = notification.userInfo as! [String:Int]
let number = info["ItemID"]
let alertController = UIAlertController(title: "本地通知",
message: "消息内容:\(notification.alertBody)用户数据:\(number)",
preferredStyle: .alert)
self.window?.rootViewController!.present(alertController, animated: true,
completion: nil)
}
全部评论(4)
我感觉应该更新下这个,现在是用 UserNotification了,创建UNNotificationContent和UNNotificationTrigger,然后在 UNNotificationRequest中加入trigger和content,接下来在UNNotificationCenter中添加Request即可。
站长回复:谢谢你的提醒,这篇文章确实写的比较早,现在已经过时了。我后面会写几篇关于UserNotification的文章更新上去。
请问这个运行allNotifications数据都为nuull 然后遍历的时候就会报错。
站长回复:第一次进去allNotifications肯定是空的。但后面使用for...in循环是不会报错的,不知道你那边是什么情况。
能麻烦解决下有关极光推送或者百度推送的原理和简单使用方法吗?
还有就是怎么让后台自动发送通知的?
程序在推出状态,怎么还会接收到消息呢?
还请赐教。。感谢!!!
站长回复:这个我也没做过,后面可以研究下, 如果有结果的话到时写篇相关文章。
请教个问题:点击APP图标响应消息通知的方法是哪个呢?
站长回复:点击APP图标就直接进入到应用里了啊。不太明白你说的“点击APP图标响应消息通知的方法”的意思,可以举个市面上常用应用的例子吗,我下下来看下