2016-08-22 15 views
6

私はこれをやってうんざりしています。何が起こっているか教えてあげてください。私はiOS 9.0 & Xcode 7.3.1を使用しています。didReceivelocalNotificationは決して呼ばれません

状況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している間にいつかのために待っていたが、いくつかのより多くの時間を待っていました。何も起こらなかった。私はアプリが通知を起動し、メソッドの1つが呼び出されると思ったので、アプリ内に通知を表示することができました。しかし、何も起こらなかった。

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

アプリの委任

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

} 
+0

あなたのアプリケーションデリゲートのスウィフトファイルコードをそのまま共有してください。 –

+0

ありがとう、 "activatingUserLocalNotification"を呼び出すコードを表示してください –

答えて

2

これは少しおかしいですが、変更:

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

へ:

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

注意資本L.ドキュメントから:

enter image description here

また:あなたのアプリがアクティブである場合にのみ、このデリゲートメソッドが呼び出されることを期待しています。あなたのアプリがアクティブなので、その状況で通知は表示されません。 didReceiveLocalNotificationデリゲートメソッドを使用して処理できます。

+0

私にチェック&戻ってみましょう。 –

+1

あなたは人間です:)ありがとう。 –

関連する問題