簡體   English   中英

didReceivelocalNotification永遠不會被調用

[英]didReceivelocalNotification is never called

我厭倦了這樣做。 讓我告訴你發生了什么事。 我使用的是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 ,這個方法會怎么做。

已經有兩天我和這個人打架了,不知道發生了什么事。

推送通知工作正常[雖然應用程序內部和外部]

讓我知道你的想法? 請。

我如何從控制器調用此函數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)
    }

App代表

//
//  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.來自docs:

在此輸入圖像描述

另外:期望只有在您的應用處於活動狀態時才會調用此委托方法。 由於您的應用處於有效狀態,因此在該情況下不會顯示通知。 您可以使用didReceiveLocalNotification委托方法來處理它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM