標籤:
上一篇講到的本地推送是普通的訊息推送,本篇要講一下帶按鈕動作的推送訊息,先上個圖瞅瞅:
繼上一篇的內容進行小小的改動:
在didFinishLaunchingWithOptions方法內進行以下修改
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 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() //最後一定要調用這上方法 }
SWIFT推送之本地推送(UILocalNotification)之二帶按鈕的訊息