永远不会调用 didReceivelocalNotification

didReceivelocalNotification is never called

我厌倦了这样做。让我告诉你发生了什么事。我正在使用 iOS 9.0 & Xcode 7.3.1.

情况一:

我像这样在didFinishLaunchingWithOptions注册了本地通知设置。

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

我的项目中有多个控制器,用户将按下其中一个中的一个按钮,我将通过调用 App Delegate 中的一个函数来安排通知。下面给出函数。

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, 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
    }

并在应用处于 Foreground 时等待了一段时间,又等了一段时间。什么都没发生。我认为该应用程序会启动通知,并且会调用其中一个方法,这样我就可以在应用程序内显示通知。但是什么也没发生。

情况二:

现在我尝试了同样的方法,现在我通过转到另一个应用程序并使用它一段时间来最小化应用程序 [应用程序处于后台状态 ]。现在通知正确启动,就像我告诉过你的那样,我已经在几乎所有方法中设置了断点,但是 none 其中的断点在我单击通知时被调用。但是它调用了 applicationWillEnterForeground,没有 launchOptions.

,这个方法该怎么办?

我已经为此苦苦挣扎了两天,不知道发生了什么事。

尽管 [应用程序内外] 推送通知工作正常

让我知道你的想法?请。

我如何从控制器调用此函数 activatingUserLocalNotification

 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)
    }

应用代理

//
//  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)
    }

}

这个有点搞笑,不过改一下:

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

收件人:

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

注意大写字母 L。来自文档:

此外:预计此委托方法仅在您的应用处于活动状态时被调用。由于您的应用程序处于活动状态,因此在这种情况下不应显示任何通知。您可以使用 didReceiveLocalNotification 委托方法来处理它。