[英]Push Local Notification when App Foreground in Swift, iOS 8,9
In IOS10, I used UNMutableNotificationContent and set delegate, 在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)")
}
}
}
send notification in foreground, the follows func was called 在前台发送通知,调用了以下func
then i can show the notification in foregound by "completionHandler" 然后我可以通过“ completionHandler”在通知中显示通知
@available(iOS 10.0, *)
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
completionHandler([.alert, .sound])
}
. 。
. 。
. 。
But in IOS8 & 9, i used UILocalNotification 但是在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)
}
and found these in AppDelegate 并在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) {
}
It's just called first func only when i send notification in foreground, but first func has no "completionHandler" what should i do? 仅当我在前台发送通知时,它才被称为第一个函子,但第一个函子没有“ completionHandler”,我该怎么办? thank your help
谢谢你的帮助
Showing native notifications is not possible in iOS previous to iOS 10 as described in this SO post . 如本SO post中所述,在iOS 10之前的iOS中无法显示本机通知。
If you need to support iOS 9, you will need to use one of the many third-party libraries that exist for this purpose. 如果需要支持iOS 9,则需要使用许多为此目的而存在的第三方库之一。
MDNotificationView is one that I created. MDNotificationView是我创建的一个。 You could pass in a custom view that mimics the look of the native notifications in iOS.
您可以传入模仿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()
}
or 要么
// If you prefer to create your view in code.
let view = YourCustomNotificationUIView()
let notificationView = MDNotificationView(view: view, position: .bottom)
notificationView.delegate = self
notificationView.show()
In addition, you can add the delegate methods: 另外,您可以添加委托方法:
// MARK: Notification View Delegate
func didTap(notificationView: MDNotificationView) {
}
func notificationDidShow(notificationView: MDNotificationView) {
}
func notificationDidHide(notificationView: MDNotificationView) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.