2016-08-22 31 views
6

denir. Bunu yapmaktan sıkıldım. Size neler olduğunu anlatayım. IOS 9.0 & Xcode 7.3.1 kullanıyorum.didReceivelocalNotification hiçbir zaman

Durum 1:

böyle didFinishLaunchingWithOptions yerel bildirim ayarları için kayıt yaptırdı.

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

Projemde birkaç denetleyicileri var, kullanıcı bunlardan birinde bir düğmeye basarak edilecek ve ben App Delegate bir işlevini çağırarak bir bildirim zamanlama olacaktır. Fonksiyon aşağıda verilmiştir.

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

Şimdi bunun başarıyla bildirim planlanan bu yukarıdaki fonksiyonu denilen bir düğmeye üzerine basarak bir bildirim belirledik.

Tüm bu yöntemlerde kesme noktaları belirledim. Uygulamanın Foreground iken

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 
    } 

Ve bekledi bazen, biraz daha zaman bekledi. Hiçbir şey olmadı. Uygulamanın bildirimi tetikleyeceğini düşündüm ve bu yöntemden biri de, uygulamanın içinde bildirimi sunabilseydim. Ama hiçbir şey olmadı.

Durum 2:

Şimdi, aynı denedi şimdi bir başka uygulamanıza gidip bazen bunu kullanarak [Background devlet yılında App] uygulamasını minimize. Şimdi bildirim düzgün bir şekilde tetiklendi, tıpkı size söylediğim gibi hemen hemen tüm yöntemlerde kesme noktaları belirledim ancak hiçbirini bildirime tıkladığım zaman aradım. Ancak applicationWillEnterForeground olarak adlandırıldı, launchOptions olmadan bu yöntemle ne yapacağız.

İki gün oldu bununla uğraşıyorum, neler olup bittiği hakkında bir fikriniz yok.

Anında İletme Bildirimleri İyi Çalışıyor [Inside İçinde & Uygulama].

Düşüncelerinizi bana bildirin. Lütfen.

Bir denetleyiciden bu işlevi nasıl çağırırım 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 Delegesi

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

Lütfen app delegate hızlı dosya kodunu olduğu gibi paylaşın. –

+0

Teşekkürler, lütfen "activatingUserLocalNotification" ifadesini gösteren kodu göster. –

cevap

2

Bu biraz komik, ama değişim:

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

için:

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

Not sermaye L.Dokümanlar:

enter image description here

Ayrıca: Bu temsilci yöntemi uygulamanız etkin olduğunda çağrılacak bekliyoruz. Uygulamanız aktif olduğundan bu durumda hiçbir bildirim görünmemelidir. DidReceiveLocalNotification temsilci yöntemini kullanarak işleyebilirsiniz.

+0

Kontrol edeyim ve geri döneyim. –

+1

Siz Man iseniz :) Çok teşekkürler. –

İlgili konular