iphoneiosobjective-cpush-notification

Get push notification while App in foreground iOS


I am using push notification service in my app. When app is in background I am able to see notification on notification screen(screen shown when we swipe down from top of iOS device). But if application is in foreground the delegate method

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo

is getting called but notification is not displayed in notification screen.

I want to show notification on notification screen independent of whether app is in background or foreground. I am tired by searching for a solution. Any help is greatly appreciated.


Solution

  • If the application is running in the foreground, iOS won't show a notification banner/alert. That's by design. But we can achieve it by using UILocalNotification as follows

    SWIFT:

    if application.applicationState == .active {
        var localNotification = UILocalNotification()
        localNotification.userInfo = userInfo
        localNotification.soundName = UILocalNotificationDefaultSoundName
        localNotification.alertBody = message
        localNotification.fireDate = Date()
        UIApplication.shared.scheduleLocalNotification(localNotification)
    }