didReceivelocalNotification永远不会被称为

时间:2016-08-22 07:34:06

标签: ios objective-c swift uilocalnotification

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

情况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时等待一段时间,等待一段时间。没啥事儿。我认为应用程序会启动通知,并且会调用其中一个方法,以便我可以在应用程序中显示通知。但什么都没发生。

情况2:

现在我尝试了相同的操作,现在我通过转到另一个应用程序并使用它一段时间来最小化应用程序[应用程序处于后台状态]。现在通知正常启动,就像我告诉你的那样,我几乎在所有方法中都设置了断点,但是当我点击通知时它们都没有调用。但它调用applicationWillEnterForeground,没有launchOptions,这个方法会怎么做。

这是我与之斗争的两天,不知道发生了什么。

推送通知工作正常[内部和外部]在App之外]

让我知道你的想法?请。

我如何从控制器调用此函数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)
    }

}

1 个答案:

答案 0 :(得分:3)

这有点好笑,但改变了:

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

要:

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

注意资本L.来自docs:

enter image description here

另外:期望只有在您的应用处于活动状态时才会调用此委托方法。由于您的应用处于有效状态,因此在该情况下不会显示通知。您可以使用didReceiveLocalNotification委托方法来处理它。