OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Swift、iOS 8,9 中应用前台时推送本地通知 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:58
标题: ios - 在 Swift、iOS 8,9 中应用前台时推送本地通知

在IOS10中,我使用了UNMutableNotificationContent并设置了委托(delegate),

private func pushNotification() {
    let content = UNMutableNotificationContent()
    content.title = "aaa"
    content.body = "bbb"
    content.subtitle = "00000"

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)

    let requestIdentifier = "com.aaaaa.bbbbb"

    let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().add(request) { error in
        if error == nil {
            print("Time Interval Notification scheduled: \\\\(requestIdentifier)")
        }
    }

}

在前台发送通知,调用了以下函数
然后我可以通过“completionHandler”在前台显示通知

@available(iOS 10.0, *)
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
    completionHandler([.alert, .sound])
}

.
.
.

但在 IOS8 和 9 中,我使用了 UILocalNotification

private func pushNotification2() {

    let notification = UILocalNotification()
    notification.alertAction = "Title Message"
    notification.alertBody = "Body Message"
    notification.fireDate = NSDate(timeIntervalSinceNow: 0) as Date
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.category = "INVITE_CATEGORY"


    UIApplication.shared.scheduleLocalNotification(notification)

}

并在 AppDelegate 中找到这些

func application(_ application: UIApplication, didReceive notification: UILocalNotification) {

}
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) {
}


func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
}

仅当我在前台发送通知时才调用第一个函数,但第一个函数没有“completionHandler” 我应该怎么办?谢谢你的帮助



Best Answer-推荐答案


如本 SO post 中所述,在 iOS 10 之前的 iOS 中无法显示 native 通知.

如果您需要支持 iOS 9,则需要使用为此目的而存在的众多第三方库之一。

MDNotificationView是我创建的。您可以传入一个模仿 iOS 中 native 通知外观的自定义 View 。

// If you prefer to create your view with Interface Builder.
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "CustomView", bundle: bundle)
let views = nib.instantiate(withOwner: self, options: nil)

if 0 < views.count,
    let view = views[0] as? UIView {        

    let notificationView = MDNotificationView(view: view, position: .bottom)
    notificationView.delegate = self
    notificationView.show()
}

// If you prefer to create your view in code.
let view = YourCustomNotificationUIView()

let notificationView = MDNotificationView(view: view, position: .bottom)
notificationView.delegate = self
notificationView.show()

此外,您还可以添加委托(delegate)方法:

// MARK: Notification View Delegate

func didTap(notificationView: MDNotificationView) {
}

func notificationDidShow(notificationView: MDNotificationView) {
}

func notificationDidHide(notificationView: MDNotificationView) {
}

关于ios - 在 Swift、iOS 8,9 中应用前台时推送本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40371125/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4