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

本文详细介绍了如何在iOS应用中配置和处理推送通知。包括针对iOS 8及以上版本的推送通知设置步骤,如创建动作、类别集合及注册推送等。同时展示了如何在应用进入后台时触发本地通知,并解释了如何通过特定方法处理用户点击推送消息的操作。

在didFinishLaunchingWithOptions方法内进行以下修改

//判断ios 版本
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方法

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 设置icon
        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) {}这个方法。

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

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
        println("identifier=\(identifier)")  //这里的identifier是按钮的identifier

        completionHandler()  //最后一定要调用这上方法
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值