Reputation: 74
I would like to add a notification banner as an addition to an existing github application. This banner would displays a warning whenever the temperature rises above 90°F or 32°C and drops below 50°F or 10°C. I am using Swift 4 and xcode 9.4 and am new app development.
I have taken a look at this guide and to these instructions to build a simple weather application, but they do not describe how to add a notification with the temperature. Is there a way to access the weather information or about the temperature from the app.
I have read about using UNUserNotificationCenterDelegate from this post and want to find a way to simple display a temperature warning. I would appreciate any code snippets that can illustrate how to accomplish this.
Upvotes: 1
Views: 1739
Reputation: 13860
There is a couple of steps you need to do before even thinking of sending a notification. But let's clear in the beginning this:
There is no code snippet for this, because it's not as trivial as it may sound. I will provide you a list of steps below and you can google tutorials for it or use links I will provide:
CLLocationManager
with Always
option, you'll need this to check temperature based on user location in background: Here is a useful link how to get permission and delegates right: https://www.techotopia.com/index.php/A_Swift_Example_iOS_8_Location_Application. That will be perfect solution for first implementation of your challenge. Later you may have a look at: Significant Location ChangeCodable
approach: https://medium.com/xcblog/painless-json-parsing-with-swift-codable-2c0beaeb21c1Happy Coding
Upvotes: 3
Reputation: 230
You need to trigger a local notification when your app is in the foreground and whenever the temperature goes above 32°C or below 10°C, From ios 10 apple allows you to send the local notification within your app while your app is running in the foreground. So here are the steps to create a local notification while your app is in background
Request Notification permission in AppDelegate application:didFinishLaunchingWithOptions:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in
})
Set UNUserNotificationCenter.current().delegate = self
Implement the UNUserNotificationCenterDelegate
protocol in your app delegate
Override the willpresent method of the UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
Trigger notification whenever the temperature goes above 32°C or below 10°C
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Simple Notification"
content.subtitle = ""
content.body = "Notification is here"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval:2.0, repeats: false)
let request = UNNotificationRequest(identifier: "ContentIdentifier", content: content, trigger: trigger)
This code creates a local notification that will be displayed to the user after 2 seconds
For more information about Notification read the apple documentation UNUserNotificationCenter UNUserNotificationCenterDelegate
Upvotes: 2