2017-05-08 29 views
3

我有一個適用於iOS的天氣應用程序,我希望允許用戶每天早上在他們選擇的時間接收通知,以便獲取天氣預報並顯示通知。Swift:如何使用動態內容安排本地通知

我想避免使用推送通知,我想我可以使用本地通知,除非我看不到從服務器獲取內容的方式。它看起來像內容必須在調度時設置。是對的嗎?

這使我想我可以註冊我的應用程序以使用後臺執行來定期獲取天氣並安排最新內容的通知,但這看起來很浪費。

總之,我想告訴iOS在特定時間運行特定的功能。我錯過了這個選擇嗎?推送通知是完成這種事情的唯一/最佳方式嗎?

+0

你可以安排與您的內容本地通知。 – KKRocks

+0

@KKRocks也許我對我的問題不夠清楚。我看到的問題是,當我顯示通知時,我想要獲取要在本地通知中顯示的內容。所以,就我而言,用戶可能會安排上午7點通知,並且我想在當時爲通知獲取內容。 –

+1

是的,然後推送通知是您的最佳選擇,如果你想顯示天氣預報。 – KKRocks

回答

1

您可以安排一個特定時間的本地通知,當用戶看到它時,他是否希望他可以通過點擊該通知來打開您的應用程序。那時,您將能夠知道,用戶已經點擊了通知,因此應用程序已經打開,您可以進行網絡調用來獲取數據並將其顯示在應用程序中。 這不需要任何背景調用,只需要用戶進行網絡呼叫。

另一種選擇:您可以創建應用程序的小部件(如天氣小工具)。無論何時用戶進入小部件區域,您都將收到代表電話並進行網絡呼叫以獲取最新的天氣數據。如果用戶想要更多的信息,他可以輕鬆點擊它,你的應用程序將打開。那麼,一切都將掌握在你的手中。

您的選擇:您可以隨時獲取動態內容,只要用戶在特定日期打開您的應用併爲其設置通知。但這並不容易,因爲用戶可能無法獲得更新的數據。

推送通知:但是,如果要通過服務器將動態數據發送到您的應用程序,可能不需要您的情況。這總是最好的選擇。

1

我創建了一個功能。當你想要的時候,這會在特定的時間調用你的函數。我創建了一個時鐘應用程序,所以我需要在用戶創建鬧鐘時觸發本地通知。在通知中心委託方法中,您可以處理您的回覆並調用您想要的任何方法。

class LocalNotificationMethod : NSObject { 

static let notificationInstance = LocalNotificationMethod() 

let requestIdentifier = "SampleRequest" //identifier is to cancel the notification request 

internal func scheduleLocalNotification(titleOfNotification:String, subtitleOfNotification:String, messageOfNotification:String, soundOfNotification:String, dateOfNotification:String) { 

    if #available(iOS 10.0, *) { 

     let formatter = DateFormatter() 
     formatter.dateFormat = "yyyy-MM-dd hh:mm a" 
     let date3 = formatter.date(from: dateOfNotification) 

     let content = UNMutableNotificationContent() 
     content.body = NSString.localizedUserNotificationString(forKey: titleOfNotification, arguments: nil) 
     content.sound = soundOfNotification.characters.count > 0 ? UNNotificationSound.init(named: soundOfNotification + ".mp3") : UNNotificationSound.default() 

     let trigger = UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.day, .month, .year, .hour, .minute], from: date3!), repeats: false) 

     let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger) 

     UNUserNotificationCenter.current().add(request){(error) in 

      if (error != nil){ 

       print(error?.localizedDescription) 
      } else { 
       print("Successfully Done") 
      } 
     } 
    } else { 
     // Fallback on earlier versions 
    } 

} 
} 

而且在AppDelegate的方法: - 您可以在您的通知,或當您的通知將present.Is取決於你想要做的事情處理每當用戶點擊。

// MARK: - 通知委託

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    print("Tapped in notification") 
} 

//This is key callback to present notification while the app is in foreground 
@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 


    print("Notification being triggered") 
    //You can either present alert ,sound or increase badge while the app is in foreground too with ios 10 
    //to distinguish between notifications 
    if notification.request.identifier == "SampleRequest" { 

     completionHandler([.alert,.sound,.badge]) 

    } 
} 
+0

謝謝,但我不知道這是如何做到我所需要的。我期望做的是(a)安排一個通知,每天早上7點發送一次,以及(b)能夠在上午7點從我的服務器獲取通知的文本。在'willPresent'函數中,你能夠從服務器獲取json等嗎? –