• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift-推送之本地推送(UILocalNotification)添加Button的点击事件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

 

import UIKit

 

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

 

    var window: UIWindow?

 

    // 本地推送通知是通过实例化UILocalNotification实现的。要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户。

    

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        

        //1.创建一组动作

        let userAction = UIMutableUserNotificationAction()

        userAction.identifier = "action"

        userAction.title = "Accept"

        userAction.activationMode = UIUserNotificationActivationMode.Foreground

        

        let userAction2 = UIMutableUserNotificationAction()

        userAction2.identifier = "action2"

        userAction2.title = "Ingore"

        userAction2.activationMode = UIUserNotificationActivationMode.Background

        userAction2.authenticationRequired = true

        userAction2.destructive = true

        

        //2.创建动作的类别集合

        let userCategory = UIMutableUserNotificationCategory()

        userCategory.identifier = "MyNotification"

        userCategory.setActions([userAction,userAction2], forContext: UIUserNotificationActionContext.Minimal)

        let categories:NSSet = NSSet(object: userCategory)

        

        //3.创建UIUserNotificationSettings,并设置消息的显示类类型

        let userSetting = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert]

            , categories: categories as? Set<UIUserNotificationCategory>)

        

        //4.注册推送

        application.registerForRemoteNotifications()

        application.registerUserNotificationSettings(userSetting)

        return true

    }

 

    // App既将进入后台、锁屏、有电话进来时会触发此事件

    func applicationWillResignActive(application: UIApplication) {

        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    }

 

    // App进入后台时触发此事件,此时在applicationDidEnterBackground方法内写入以下代码:

    // 此时将按Home键将App切换到后台时会有一条推送消息,App角标变为了“1”

    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()

        

        let notification = UILocalNotification()

        //notification.fireDate = NSDate().dateByAddingTimeInterval(1)

        //setting timeZone as localTimeZone

        notification.timeZone = NSTimeZone.localTimeZone()

        notification.repeatInterval = NSCalendarUnit.Day

        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)

    }

    

    /*

    点击推送消息的按钮时会触发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) {

        print("identifier=\(identifier)"//这里的identifier是按钮的identifier

        

        completionHandler()  //最后一定要调用这上方法

        

        UIApplication.sharedApplication().cancelAllLocalNotifications()

        let userInfo = notification.userInfo!

        let title = userInfo["key"] as! String

        let alert = UIAlertView()

        alert.title = title

        alert.message = notification.alertBody

        alert.addButtonWithTitle(notification.alertAction!)

        alert.cancelButtonIndex = 0

        alert.show()

    }

    

    //  App从后台即将回到前台时触发此事件

    func applicationWillEnterForeground(application: UIApplication) {

        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

    }

 

    // App变成活动状态时触发此事件

    func applicationDidBecomeActive(application: UIApplication) {

        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

        // 当程序处于活动状态的时候清除ICON的角标

        application.cancelAllLocalNotifications()

        application.applicationIconBadgeNumber = 0

    }

 

    // App退出时触发此方法,一般用于保存某些特定的数据

    func applicationWillTerminate(application: UIApplication) {

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    }

 

}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift-UIPickerView的使用发布时间:2022-07-13
下一篇:
基于Swift3+WKWebView的WebView壳程序开发发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap