简体   繁体   English

在Swift,iOS 8,9中的App Foreground时推送本地通知

[英]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.

相关问题 当应用程序在前台时,iOS Swift 接收推送通知 - iOS Swift Receive Push Notification When App in Foreground 当应用程序处于前台时获取本地通知 Swift 4 iOS 11 - Get local notification when App is in foreground Swift 4 iOS 11 有什么方法可以在 iOS Swift 5 的前台应用程序时获得推送通知? - Is there any way to get push notification while App in foreground on iOS Swift 5? 当应用程序处于前台时禁用一个信号推送通知 ios 10 swift - Disable One Signal Push Notification when app is in foreground ios 10 swift 当应用程序从 iOS 中的本地通知进入前台时触发特定操作? (使用迅捷) - Triggering a specific action when the app enters foreground from a local notification in iOS? (using swift) 应用在前台运行时收到 iOS 推送通知 - iOS push notification received when app is running in foreground App在前景上并收到推送通知消息(IOS)时发生钛合金崩溃 - Titanium Crash when App is on Foreground and receives a Push Notification message (IOS) 当应用程序处于前台时,Appcelerator iOS 推送通知不起作用 - Appcelerator iOS Push notification not working when app is in Foreground Flutter iOS 应用程序在前台时的 FCM 推送通知 - Flutter FCM push notification for iOS when app is in foreground iOS如何在应用程序处于前台时处理解析JSON推送通知 - iOS How to Handle Parse JSON Push Notification when App is in Foreground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM