日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()? //最后一定要調用這上方法 ????}

轉載于:https://www.cnblogs.com/Free-Thinker/p/6478650.html

總結

以上是生活随笔為你收集整理的SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。