栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在应用程序处于前台Swift 3时获取本地通知以显示

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在应用程序处于前台Swift 3时获取本地通知以显示

有一种委托方法可在iOS 10中打开应用程序时显示通知。您必须实现此方法,才能在应用程序打开时使丰富的通知正常工作。

extension ViewController: UNUserNotificationCenterDelegate {    //for displaying notification when app is in foreground    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {        //If you don't want to show notification when app is open, do something here else and make a return here.         //Even you you don't implement this delegate method, you will not see the notification on the specified controller. So, you have to implement this delegate and make sure the below line execute. i.e. completionHandler.        completionHandler([.alert, .badge, .sound])     }    // For handling tap and user actions    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {        switch response.actionIdentifier {        case "action1": print("Action First Tapped")        case "action2": print("Action Second Tapped")        default: break        }        completionHandler()    }}

为了在iOS 10中安排通知并提供徽章

override func viewDidLoad() {    super.viewDidLoad()    // set UNUserNotificationCenter delegate to self    UNUserNotificationCenter.current().delegate = self    scheduleNotifications()}func scheduleNotifications() {    let content = UNMutableNotificationContent()    let requestIdentifier = "rajanNotification"    content.badge = 1    content.title = "This is a rich notification"    content.subtitle = "Hello there, I am Rajan Maheshwari"    content.body = "Hello body"    content.categoryIdentifier = "actionCategory"    content.sound = UNNotificationSound.default    // If you want to attach any image to show in local notification    let url = Bundle.main.url(forResource: "notificationImage", withExtension: ".jpg")    do {        let attachment = try? UNNotificationAttachment(identifier: requestIdentifier, url: url!, options: nil)        content.attachments = [attachment!]    }    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 3.0, repeats: false)    let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)    UNUserNotificationCenter.current().add(request) { (error:Error?) in        if error != nil { print(error?.localizedDescription ?? "some unknown error")        }  print("Notification Register Success")    }}

为了在AppDelegate中注册,我们必须在

didFinishLaunchingWithOptions

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {        // Override point for customization after application launch.        registerForRichNotifications()        return true    }

我也在这里定义了动作。您可以跳过它们

func registerForRichNotifications() {       UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (granted:Bool, error:Error?) in if error != nil {     print(error?.localizedDescription) } if granted {     print("Permission granted") } else {     print("Permission not granted") }        }        //actions defination        let action1 = UNNotificationAction(identifier: "action1", title: "Action First", options: [.foreground])        let action2 = UNNotificationAction(identifier: "action2", title: "Action Second", options: [.foreground])        let category = UNNotificationCategory(identifier: "actionCategory", actions: [action1,action2], intentIdentifiers: [], options: [])        UNUserNotificationCenter.current().setNotificationCategories([category])    }

如果希望将通知标题显示在整个应用程序中的任何位置,则可以编写

UNUserNotificationDelegate
in
的委托
AppDelegate
并使
UNUserNotificationCenter
当前委托成为
AppDelegate

extension AppDelegate: UNUserNotificationCenterDelegate {    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {        print(response.notification.request.content.userInfo)        completionHandler()    }    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {        completionHandler([.alert, .badge, .sound])     }}

检查此链接以获取更多详细信息

Github示例
https://github.com/kenechilearnspre/UserNotificationsTutorial

这是输出



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392097.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号