2

In IOS10, I used UNMutableNotificationContent and set 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)")
        }
    }

}

send notification in foreground, the follows func was called
then i can show the notification in foregound by "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

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

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? thank your help

pkamb
  • 33,281
  • 23
  • 160
  • 191
Water Magical
  • 979
  • 1
  • 9
  • 13

1 Answers1

0

Showing native notifications is not possible in iOS previous to iOS 10 as described in this SO post.

If you need to support iOS 9, you will need to use one of the many third-party libraries that exist for this purpose.

MDNotificationView is one that I created. You could pass in a custom view that mimics the look of the native notifications in 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) {
}
Community
  • 1
  • 1
Paul
  • 999
  • 1
  • 12
  • 29