2017-02-28 50 views
0

那麼,現有的討論都沒有爲我提供一個可行的解決方案。所以這裏是我的代碼來顯示本地通知。我錯過了什麼?UILocalNotifications不在預定時觸發

let notification = UILocalNotification() 
notification.alertBody = "Reminder" // text that will be displayed in the notification 
notification.alertAction = "open" 
notification.applicationIconBadgeNumber = 1 
notification.soundName = UILocalNotificationDefaultSoundName 


notification.fireDate = NSDate(timeIntervalSinceNow: 5) 
print("Notification scheduled") 

notification.userInfo = ["title": "Test", "UUID": "1"] 
notification.repeatInterval = NSCalendarUnit.WeekOfMonth 

UIApplication.sharedApplication().scheduleLocalNotification(notification) 

我知道,當應用程序在前臺應該有一個事件didReceiveLocalNotification。我沒有在appdelegate的didReceiveLocalNotification或通知中獲取事件。但是,當我使用presentLocalNotificationNow與通知 - 我的應用程序委託didReceiveLocalNotification確實被調用。我也嘗試與其他fireDates,但它不起作用。我錯過了什麼?

哦,我有下面的代碼在我的appdelegate didfinishlaunchingapplication

讓應用= UIApplication.sharedApplication()

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

iOS 10中的本地通知需要用戶授權。你有代碼獲取授權嗎? - 你現在應該使用UNNotificationRequest,而不是UILocalNotification。 – matt

+0

是的,我要求授權,並且在我的帖子中。 –

+0

關於UNNotificationRequest我無法使用它,因爲我支持iOS 9以及 –

回答

2

我已經測試了,這是我的結果,這個問題似乎與時間單位weekOfMonth與相關如預期day作品,這裏是控制檯日誌的圖片,使用的XCode 8.2,設備的iOS 10.2,仍在工作

enter image description here

func scheduleTestNotification() 
{ 
    let notification = UILocalNotification() 
    notification.alertBody = "Reminder" // text that will be displayed in the notification 
    notification.alertAction = "open" 
    notification.applicationIconBadgeNumber = 1 
    notification.soundName = UILocalNotificationDefaultSoundName 

    notification.timeZone = NSTimeZone.default 
    notification.fireDate = Date(timeIntervalSinceNow: 5) 
    print("Notification scheduled") 

    notification.userInfo = ["title": "Test", "UUID": "1"] 
    notification.repeatInterval = NSCalendar.Unit.day 
    UIApplication.shared.scheduleLocalNotification(notification) 
    debugPrint(UIApplication.shared.scheduledLocalNotifications!) 
} 

,但如果我用weekOfMonth然後

enter image description here

我想你可以使用此代碼作爲一種解決辦法,這不是最好的解決辦法,但也許可以幫你實現你所需要的

func scheduleTestNotification() 
    { 
     let notification = UILocalNotification() 
     notification.alertBody = "Reminder" // text that will be displayed in the notification 
     notification.alertAction = "open" 
     notification.applicationIconBadgeNumber = 1 
     notification.soundName = UILocalNotificationDefaultSoundName 

     notification.timeZone = NSTimeZone.default 
     notification.fireDate = Date(timeIntervalSinceNow: 5) 
     print("Notification scheduled") 

     notification.userInfo = ["title": "Test", "UUID": "1"] 

     UIApplication.shared.scheduleLocalNotification(notification) 
     notification.repeatInterval = NSCalendar.Unit.day 
     notification.fireDate = Calendar.current.date(byAdding: .day, value: 7, to: notification.fireDate!) 
     UIApplication.shared.scheduleLocalNotification(notification) 
     debugPrint(UIApplication.shared.scheduledLocalNotifications!) 
    } 

我希望這有助於你

0

我測試,此代碼的工作就在我身邊,iOS設備10的XCode 8.2

let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert ], categories: nil) 
    UIApplication.shared.registerUserNotificationSettings(settings) 

    let notification = UILocalNotification() 
    notification.alertBody = "Reminder" // text that will be displayed in the notification 
    notification.alertAction = "open" 
    notification.applicationIconBadgeNumber = 1 
    notification.soundName = UILocalNotificationDefaultSoundName 


    notification.fireDate = Date(timeIntervalSinceNow: 5) 
    print("Notification scheduled") 

    notification.userInfo = ["title": "Test", "UUID": "1"] 
    notification.repeatInterval = NSCalendar.Unit.minute 

    UIApplication.shared.cancelAllLocalNotifications() 
    UIApplication.shared.scheduleLocalNotification(notification) 

每當應用程序推送本地通知的方法didReceiveNotification被調用。我的repeatInterval是分鐘而不是weekOfMonth。我認爲weekOfMonth是問題所在,因爲當我將repeatInterval更改爲weekOfMonth時,我沒有收到通知,didReceiveNotification方法從未調用過。

1

要在iOS 10中發送通知,在觸發通知之前,您需要先獲得許可。

func requestPermition() { 
     let center = UNUserNotificationCenter.current() 
     center.getNotificationSettings { (settings) in 
      if settings.authorizationStatus != .authorized { 

       let options: UNAuthorizationOptions = [.alert, .sound]; 
       center.requestAuthorization(options: options) { 
        (granted, error) in 
        if !granted { 
         print("Something went wrong") 
        }else { 
         print("permition granted") 
        } 
       } 
      } 
     } 
    } 

UILocalNotification在iOS的10已被棄用,你可以使用UNUserNotificationCenter instead.to解僱的通知嘗試婁代碼

//to fire notification after 5 seconds. 
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, 
                repeats: false) 

    //Set contents to be desplayed on the notification 
    let content = UNMutableNotificationContent() 
    content.title = "Reminder" 
    content.body = "Test reminder" 
    content.sound = UNNotificationSound.default() 
    content.categoryIdentifier = "myCategory" 


    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger) 

    //set UNNotificationActionOptions .foreground if you need to open when button tapped. 
    let action = UNNotificationAction(identifier: "remindOpen", title: "Open", options: [.foreground]) 
    let category = UNNotificationCategory(identifier: "myCategory", actions: [action], intentIdentifiers: [], options: []) 

    UNUserNotificationCenter.current().setNotificationCategories([category]) 
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 
    UNUserNotificationCenter.current().add(request) {(error) in 
     if let error = error { 
      print("notification error: \(error)") 
     } 
    } 
0

所以,我也遇到了同樣的問題。 我試圖從背景獲取中觸發UILocalNotification。我已經安排了UILocalNotification,我在Xcode中進行了一些操作,確保調用了scheduleLocalNotification,但它只發射了大約1/5次。

似乎從後臺獲取調度UINotification根本無法在iOS 10中可靠地工作。我切換到使用UNUserNotification,並且每當我遍歷代碼時它都會可靠地啓動。