YouSS
YouSS

Reputation: 570

Display notification alert when the app is in foreground

I can get the push notification message in this method :

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
} 

But the problem is how to display an alert notification when the app is runing as the same way it displayed when the app is in background.

Is that possible ?

Upvotes: 0

Views: 1822

Answers (3)

Furkan S. Dağ
Furkan S. Dağ

Reputation: 61

Adding that completionHandler line to appDelegate method worked for me:

//Called when a notification is delivered to a foreground app.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("User Info = ",notification.request.content.userInfo)

completionHandler([.alert, .badge, .sound])
} 

Upvotes: 2

oren
oren

Reputation: 3199

When the app in foreground, you only get the callback, iOS doesn't show alert in that case, you have to do it yourself...

You can do it like this:

Create a nib for the 'PN foreground view', for example:

enter image description here

And when you get a PN in foreground, you can instantiate the view, and add it to the UIWindow with animation, code for example:

        // init and configure your custom PN foreground view
        let nib = UINib(nibName: self.nibName, bundle: NSBundle(forClass: PnForegroundView))
        pnForegroundView = nib.instantiateWithOwner(nil, options: nil)[0] as! PnForegroundView
        pnForegroundView.setWidth(UIScreen.width)
        pnForegroundView.setHeight(63)
        pnForegroundView.title = <some title from notification>
        pnForegroundView.image = <some image from notification>

        // add the view to the key window 
        let window = UIApplication.sharedApplication().keyWindow!
        window.addSubview(pnForegroundView!)

        // Change window level to hide the status bar
        window.windowLevel = UIWindowLevelStatusBar

        // Show the PN foreground view with animation:

        self.pnForegroundView!.setBottom(0)
        self.changeWindowLevelToHideStatusBar()
        UIView.animateWithDuration(0.2) {
            self.pnForegroundView!.setBottom(self.pnForegroundView!.height)
        }

Of course, you should set a delegate for this view, for case user clicking the notification, and when the user dismisses it.

Also, you can add time for automatic dismissal.

Last thing - when removing the PnForegroundView, you better reset your UIWindow level to default value, for showing the status bar

Upvotes: 2

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20379

didReceiveRemoteNotification will trigger no matter whether you are foreground or in background Though, you can handle them seperately as below

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        let state = UIApplication.sharedApplication().applicationState
        if state == UIApplicationState.Active {
            //show alert here your app is in foreground
        }
        else{
            //your app is in background
        }

    }

Upvotes: 1

Related Questions