iosLearner
iosLearner

Reputation: 1331

UILocalNotifcation not fired in swift 2.0

I am working Push notification and i have done all steps to setup push notification.

i can able to receive notification when application in background but when application in foreground its landing on didReceiveRemoteNotification but its not firing.

here my code in AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        if application.applicationState == UIApplicationState.Active {
            let localNotification = UILocalNotification()
            let date = NSDate(timeIntervalSinceNow: 10)
            localNotification.fireDate = date
            let timeZone = NSTimeZone.localTimeZone()
            localNotification.timeZone = timeZone
            localNotification.alertBody = "Sample Notification Body"
            localNotification.userInfo = userInfo
            print(localNotification)
            localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
        }
    }

What mistake i am doing ??

Thanks in advance

Upvotes: 0

Views: 77

Answers (2)

Kukoo Ch
Kukoo Ch

Reputation: 11

When the application is in Foreground and running, it does not show notification banner. Banner alert is only shown when Application is either in background(inactive) or is not running at all. The delegate method

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

is called for Remote(Push) Notification and

application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)

is called for local notification.

You can use any 3rd party library bryx-inc/BRYXBanner to show a banner for your notification alert message . The delegate method didReceiveRemoteNotification can be used to update badge icon and show message in banner view.

Upvotes: 0

Adil Soomro
Adil Soomro

Reputation: 37729

What I suspect is, you're trying to show the remote notification when the app is active using local notification.

Well, either its local notification or remote, you can't see any UI alert when the app is active, instead the delegate methods for local or remote notification are called accordingly.

However iOS 10 adds the ability to view it inside the app. Here's the discussion: https://stackoverflow.com/a/37844312/593709

For showing any alert on pre-iOS-10, while your app is active, you need to use UIAlertController or some other implementation like MPNotificationView.

Upvotes: 1

Related Questions