【问题标题】:Push Local Notification when App Foreground in Swift, iOS 8,9在 Swift、iOS 8,9 中应用前台时推送本地通知
【发布时间】:2017-03-15 05:52:25
【问题描述】:

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

}

在前台发送通知,调用了以下函数
然后我可以通过“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” 我该怎么办?谢谢你的帮助

【问题讨论】:

    标签: ios swift localnotification


    【解决方案1】:

    如本 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) {
    }
    

    【讨论】:

    • MDNotificationView 不再可用..你能帮我提供 MDNotificationView 代码吗。
    猜你喜欢
    • 2019-06-26
    • 1970-01-01
    • 2017-05-11
    • 2018-09-19
    • 2014-10-21
    • 2013-01-30
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多