SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息

上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息,先上个图瞅瞅:

继上一篇的内容进行小小的改动:

在didFinishLaunchingWithOptions方法内进行以下修改

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
if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8 {
//            APService.registerForRemoteNotificationTypes(
//                UIUserNotificationType.Badge.rawValue |
//                UIUserNotificationType.Sound.rawValue |
//                UIUserNotificationType.Alert.rawValue,
//                categories: setting.categories)
             
            //1.创建一组动作
            var userAction = UIMutableUserNotificationAction()
            userAction.identifier = "action"
            userAction.title = "Accept"
            userAction.activationMode = UIUserNotificationActivationMode.Foreground
             
            var userAction2 = UIMutableUserNotificationAction()
            userAction2.identifier = "action2"
            userAction2.title = "Ingore"
            userAction2.activationMode = UIUserNotificationActivationMode.Background
            userAction2.authenticationRequired = true
            userAction2.destructive = true
             
            //2.创建动作的类别集合
            var userCategory = UIMutableUserNotificationCategory()
            userCategory.identifier = "MyNotification"
            userCategory.setActions([userAction,userAction2], forContext: UIUserNotificationActionContext.Minimal)
            var categories:NSSet = NSSet(object: userCategory)
             
            //3.创建UIUserNotificationSettings,并设置消息的显示类类型
            var userSetting = UIUserNotificationSettings(forTypes:
                    UIUserNotificationType.Badge |
                    UIUserNotificationType.Sound |
                    UIUserNotificationType.Alert
                , categories: categories as Set<NSObject>)
             
            //4.注册推送
            application.registerForRemoteNotifications()
            application.registerUserNotificationSettings(userSetting)
         
        }

 2.修改applicationDidEnterBackground方法

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
func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        UIApplication.sharedApplication().cancelAllLocalNotifications()
         
        var notification = UILocalNotification()
        //notification.fireDate = NSDate().dateByAddingTimeInterval(1)
        //setting timeZone as localTimeZone
        notification.timeZone = NSTimeZone.localTimeZone()
        notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
        notification.alertTitle = "This is a local notification"
        notification.alertBody = "Hey,It's great to see you again"
        notification.alertAction = "OK"
        notification.category = "MyNotification" //这个很重要,跟上面的动作集合(UIMutableUserNotificationCategory)的identifier一样
        notification.soundName = UILocalNotificationDefaultSoundName
        //setting app's icon badge
        notification.applicationIconBadgeNumber = 1
         
        var userInfo:[NSObject : AnyObject] = [NSObject : AnyObject]()
        userInfo["kLocalNotificationID"] = "LocalNotificationID"
        userInfo["key"] = "Attention Please"
        notification.userInfo = userInfo
         
        //UIApplication.sharedApplication().scheduleLocalNotification(notification)
        //UIApplication.sharedApplication().presentLocalNotificationNow(notification)
        application.presentLocalNotificationNow(notification)
    }

 3.点击推送消息的按钮时会触发func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {}这个方法。

如果是远程推送那就是func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], completionHandler: () -> Void) {}这个方法。

这里只需要调用本地第一个方法即可

 

1
2
3
4
5
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
        println("identifier=\(identifier)"//这里的identifier是按钮的identifier
         
        completionHandler()  //最后一定要调用这上方法
    }
posted @   brave-sailor  阅读(215)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2016-02-28 Android 4.2蓝牙介绍
2015-02-28 Attempt to call getDuration without a valid mediaplayer
2015-02-28 Mac环境下svn的使用
2015-02-28 MediaController
2014-02-28 Android进阶2之APK方式换肤
2014-02-28 Android APK方式换肤实现原理
点击右上角即可分享
微信分享提示