加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift - 本地消息的推送通知(附样例)

发布时间:2020-12-14 07:14:19 所属栏目:百科 来源:网络整理
导读:使用UILocalNotification可以很方便的实现消息的推送功能。我们可以设置这个消息的推送时间,推送内容等。 当推送时间一到,不管用户在桌面还是其他应用中,屏幕上方会都显示出推送消息。 1,推送消息的发送 --- AppDelegate.swift --- import UIKit @UIAppli
使用UILocalNotification可以很方便的实现消息的推送功能。我们可以设置这个消息的推送时间,推送内容等。
当推送时间一到,不管用户在桌面还是其他应用中,屏幕上方会都显示出推送消息。

1,推送消息的发送


--- AppDelegate.swift ---
import UIKit
@UIApplicationMain
class AppDelegate : UIResponder , UIApplicationDelegate {
var window: UIWindow ?
func application(application: UIApplication didFinishLaunchingWithOptions launchOptions: [ NSObject : AnyObject ]?) -> Bool {
//开启通知
let settings = UIUserNotificationSettings (forTypes: [. Alert Badge Sound ],
categories: nil )
application.registerUserNotificationSettings(settings)
return true
}
applicationWillResignActive(application: ) {
}
applicationDidEnterBackground(application: ) {
}
applicationWillEnterForeground(application: ) {
}
applicationDidBecomeActive(application: ) {
}
applicationWillTerminate(application: ) {
}
}

--- ViewController.swift ---
ViewController UIViewController {
override viewDidLoad() {
super .viewDidLoad()
//发送通知消息
scheduleNotification(12345);
//清除所有本地推送
//UIApplication.sharedApplication().cancelAllLocalNotifications()
}
//发送通知消息
scheduleNotification(itemID: Int ){
//如果已存在该通知消息,则先取消
cancelNotification(itemID)
//创建UILocalNotification来进行本地消息通知
localNotification = UILocalNotification ()
//推送时间(设置为30秒以后)
localNotification.fireDate = NSDate (timeIntervalSinceNow: 30)
//时区
localNotification.timeZone = NSTimeZone .defaultTimeZone()
//推送内容
localNotification.alertBody = "来自hangge.com的本地消息"
//声音
localNotification.soundName = UILocalNotificationDefaultSoundName
//额外信息
localNotification.userInfo = [ "ItemID" :itemID]
.sharedApplication().scheduleLocalNotification(localNotification)
}
//取消通知消息
cancelNotification(itemID: ){
//通过itemID获取已有的消息推送,然后删除掉,以便重新判断
existingNotification = self .notificationForThisItem(itemID) as ?
if existingNotification != {
//如果existingNotification不为nil,就取消消息推送
.sharedApplication().cancelLocalNotification(existingNotification!)
}
}
//通过遍历所有消息推送,通过itemid的对比,返回UIlocalNotification
notificationForThisItem(itemID: )-> ? {
allNotifications = .sharedApplication().scheduledLocalNotifications
for notification in allNotifications! {
info = notification.userInfo as ! [ String ]
number = info[ ]
number != && number == itemID {
return UILocalNotification
}
}
nil
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

2,点击推送消息的响应
收到推送,如果点击推送内容,则会重新进入到App,这个时候会调用AppDelegate中的func application(application: UIApplication,didReceiveLocalNotification notification: UILocalNotification)代理方法。
在这个方法中我们可以根据推送的消息内容实现相关的功能。

didReceiveLocalNotification notification: ) {
//设定Badge数目
.sharedApplication().applicationIconBadgeNumber = 0
]
]
alertController = UIAlertController (title: "本地通知" message: "消息内容:(notification.alertBody)用户数据:(number)" preferredStyle: UIAlertControllerStyle . )

let cancel = UIAlertAction(title: "取消",style: UIAlertActionStyle.Cancel,handler: nil);

alertController.addAction(cancel);

.window?.rootViewController!.presentViewController(alertController,
animated: true )
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读