Alvin Varghese Alvin Varghese - 3 months ago 33
iOS Question

didReceivelocalNotification is never called

I am tired of doing this. Let me tell you whats happening. I am using iOS 9.0 & Xcode 7.3.1.

Situation 1 :

I have registered for local notification settings in

didFinishLaunchingWithOptions
like this.

let settings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] , categories: nil)
application.registerUserNotificationSettings(settings)


I have several controllers in my project, user will be pressing one button in one of these and I will be scheduling a notification by calling a function in
App Delegate
. The function is given below.

func activatingUserLocalNotification(timeIntervalSinceNow : NSDate?, alertBody : String, userInfo : [NSObject : AnyObject], region : CLRegion?)
{
let localNotification = UILocalNotification()
localNotification.fireDate = timeIntervalSinceNow
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.alertBody = alertBody
localNotification.region = region
localNotification.regionTriggersOnce = false
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.userInfo = userInfo
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}


Now I have set a notification by pressing on a button, which called this above function it successfully scheduled the notification.

I have set breakpoints in all these methods.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// Break Point
return true
}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {

// Break Point
}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {

// Break Point
}

func application(application: UIApplication, didReceivelocalNotification notification: UILocalNotification)
{
// Break Point
}


And waited for sometime while the app is in
Foreground
, Waited some more time. Nothing happened. I thought the app would fire up the notification and one of the method would get called, so that I could present the notification inside the app. But nothing happened.

Situation 2 :

Now I have tried the same, now I minimised the app [ App is in Background state ] by going to an another app and using it for sometime. Now the notification fired up properly, just like I have told you I have set breakpoints in almost all methods but none of them called when I clicked on the notification. But it called
applicationWillEnterForeground
, What am gonna do with this method without the
launchOptions
.

It's been two days I am fighting with this, Don't have any idea about whats happening.

Push Notifications Works Fine Though [ Inside & Outside the App ].

Let me know your thoughts? Please.

How I call this function
activatingUserLocalNotification
from a controller.

func setLocalNotificationForDistance(id : String, name : String, location : CLLocationCoordinate2D, radius : Double)
{
let alertBody = "Hello \(name)"
let dict : [NSObject : AnyObject] = ["aps" : ["alert" : alertBody], “id” : id]
let region = CLCircularRegion(center: location, radius: radius, identifier: id)

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.activatingUserLocalNotification(nil, alertBody: alertBody, userInfo: dict, region: region)
}


App Delegate

//
// AppDelegate.swift
//
//

import UIKit
import FBSDKCoreKit
import SVProgressHUD
import Alamofire
import GoogleMaps

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

//MARK: Local Variables

var window: UIWindow?

static let UpdateRootNotification = "UpdateRootNotification"
static let ShowMainUINotification = "ShowMainUINotification"

//MARK: Application Life Cycle

// Did Finish Launching

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{

let settings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] , categories: nil)
application.registerUserNotificationSettings(settings)

return true
}

// Foreground

func applicationWillEnterForeground(application: UIApplication) {


}

// Did Become Active

func applicationDidBecomeActive(application: UIApplication) {
FBSDKAppEvents.activateApp()
application.applicationIconBadgeNumber = 0 // clear badge icon
}

// Did Enter Background

func applicationDidEnterBackground(application: UIApplication) {
}

// Opened Via Shortcut

func application(application: UIApplication, performActionForShortcutItem
shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {

}

// Continue User Activity

func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity,
restorationHandler: ([AnyObject]?) -> Void) -> Bool {

return true
}



//MARK: Local Notification

func activatingUserLocalNotification(timeIntervalSinceNow : NSDate?, alertBody : String, userInfo : [NSObject : AnyObject], region : CLRegion?)
{
let localNotification = UILocalNotification()
localNotification.fireDate = timeIntervalSinceNow
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.alertBody = alertBody
localNotification.region = region
localNotification.regionTriggersOnce = false
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.userInfo = userInfo
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {


}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {


}

func application(application: UIApplication, didReceivelocalNotification notification: UILocalNotification)
{
application.applicationIconBadgeNumber = 0

UIApplication.sharedApplication().presentLocalNotificationNow(notification)

}

// MARK: Push Notification

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

if API.sharedInstance.isLoggedIn() {
NSUserDefaults.standardUserDefaults().setObject(deviceToken, forKey: "push_token")
NSUserDefaults.standardUserDefaults().synchronize()

API.sharedInstance.registerDeviceToken(deviceToken)
}
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
application.registerForRemoteNotifications()
}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
EventTracker.trackEventWithCategory("APN", action: "Registraion", label: "Failed")
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
APNSManager.sharedInstance.handlePushNotification(userInfo)
}

//MARK: Open URL

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL:url,sourceApplication:sourceApplication,annotation:annotation)
}

}

Answer

This is a bit funny, but change:

func application(application: UIApplication, didReceivelocalNotification notification: UILocalNotification)

To:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)

Note the capital L. From docs:

enter image description here

Also: expect this delegate method to be called only when your app is active. No notification should be visible in that situation as your app is active. You can handle it by using didReceiveLocalNotification delegate method.