在Swift,iOS 8,9中使用App Foreground推送本地通知

时间:2016-11-02 01:26:41

标签: ios swift localnotification

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

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)")
        }
    }

}

在前台发送通知,后面的func被称为
然后我可以通过“completionHandler”

在foregound中显示通知
@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) {
}

只有当我在前台发送通知时它才被称为第一个func,但是第一个func没有“completionHandler” 我该怎么办?谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

如此SO post中所述,在iOS 10之前的iOS中无法显示原生通知。

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

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

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

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

// MARK: Notification View Delegate

func didTap(notificationView: MDNotificationView) {
}

func notificationDidShow(notificationView: MDNotificationView) {
}

func notificationDidHide(notificationView: MDNotificationView) {
}
相关问题