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

使用UILocalNotification可以很方便的实现消息的推送功能。我们可以设置这个消息的推送时间,推送内容等。
当推送时间一到,不管用户在桌面还是其他应用中,屏幕上方会都显示出推送消息。

1,推送消息的发送
    

--- AppDelegate.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
     }
 
     func applicationWillResignActive(application: UIApplication ) {
     }
 
     func applicationDidEnterBackground(application: UIApplication ) {
     }
 
     func applicationWillEnterForeground(application: UIApplication ) {
     }
 
     func applicationDidBecomeActive(application: UIApplication ) {
     }
 
     func applicationWillTerminate(application: UIApplication ) {
     }
}

--- ViewController.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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: 30)
         //时区
         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 = 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, didReceiveLocalNotification notification: UILocalNotification)代理方法。
在这个方法中我们可以根据推送的消息内容实现相关的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func application(application: UIApplication ,
     didReceiveLocalNotification notification: UILocalNotification ) {
         //设定Badge数目
         UIApplication .sharedApplication().applicationIconBadgeNumber = 0
             
         let info = notification.userInfo as ! [ String : Int ]
         let number = info[ "ItemID" ]
             
         let alertController = UIAlertController (title: "本地通知" ,
                 message: "消息内容:\(notification.alertBody)用户数据:\(number)" ,
                 preferredStyle: UIAlertControllerStyle . Alert )
             
         self .window?.rootViewController!.presentViewController(alertController,
                 animated: true , completion: nil )
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值