0

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)
Tarek
  • 783
  • 10
  • 31
  • That's expected. In iOS 9 there is no native framework to help you show notifications when app is in foreground! – mfaani Sep 17 '17 at 14:36
  • 1
    If you want just mimic something similar, see [here](https://stackoverflow.com/a/23365259/5175709) – mfaani Sep 17 '17 at 14:37
  • @Honey What about iOS 10? Can I display the banner when the app is in the foreground and even add action buttons to it? – Tarek Sep 17 '17 at 15:07
  • in iOS 10, you can. You have to use [`userNotificationCenter(_:willPresent:withCompletionHandler:)`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter) See [here](https://stackoverflow.com/a/44705892/5175709) – mfaani Sep 17 '17 at 15:13
  • @Honey I'm surprised though, how does Whatsapp manage to do it in iOS 9? It completely looks like a regular notification and it has a textfield where you can reply to a message, whether you're in the app or outside. – Tarek Sep 17 '17 at 15:44
  • @Tarek the notification if app is in foreground different if app in background in whats'app – Hosny Sep 17 '17 at 15:54
  • @Hosny They are exactly the same on iOS 9, even with the textField and the Send button. – Tarek Sep 17 '17 at 16:00
  • @Tarek no in foreground they use custom notification but if in background they use action notification feature – Hosny Sep 17 '17 at 16:01
  • @Hosny Oh so you mean they completely mimic the stock notification when the app is in foreground? Do you know if it's CRToast or a different library? – Tarek Sep 17 '17 at 16:02
  • @Tarek for whats'app sure they use their library but there others library like this https://github.com/bryx-inc/BRYXBanner – Hosny Sep 17 '17 at 16:05

0 Answers0