My app currently receives remote notifications and the AppDelegate forwards the payload to the app's SWRevealViewController which handles where the notification should send the user to, when the user taps it. This of coarse works when the app is either in background or inactive.
Now comes the foreground case. I tried putting this code which gets triggered when a remote notification is received AND the app is in foreground:
func displayLocalNotification() {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "iOS 10 title"
content.body = "iOS 10 body."
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default()
var dateComponents = DateComponents()
dateComponents.hour = 15
dateComponents.minute = 49
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
} else {
// ios 9
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 5) as Date
notification.alertBody = "iOS 9 Body"
notification.alertAction = "iOS 9 Action"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
}
}
I'm testing on an iPhone 4s at the moment with iOS 9 and the notification will not appear INSIDE the app (like Facebook and Whatsapp, etc) but it does appear in the Notification Center.
Am I missing something?
Some things I will eventually need to implement:
- Action buttons in the notification like Cancel and Accept Friend Request, for all the cases (foreground, background and inactive)
- Notifications when the app is completely not running (killed/not running)