Swift: how to schedule local notifications with dynamic content

I have a weather app for iOS and I would like the user to receive a notification every morning of their choice, which will receive the weather forecast for the day and display the notification.

I'd like to avoid using push notifications, and I thought I could use local notifications, except I don't see a way to get content to be shown from the server. It looks like the content needs to be set during scheduling. Is it correct?

This makes me think that I can register my app to use background execution to periodically receive a weather forecast and schedule a notification with the latest content, but that seems wasteful.

In short, I would like to say that iOS triggers a specific function at a specific time. Is there a good option for this that I am missing? Are push notifications the only / best way to achieve this kind of thing?

+3


source to share


3 answers


Push notification is the best option for yours if you want to display weather forecast

.



More on this: fooobar.com/questions/2409253 / ...

0


source


You can schedule a local notification for a specific time and when the user sees it, and if they want, they can open your app by clicking on that notification. At that time you will be able to find out that the user has clicked on the notification and so the application is open, you can make a network call to fetch the data and display it inside the application. This does not require any background calls to do this and only makes a network call to the user action.

Alternatively, you can create a widget for your application (like Weather Widget). Whenever the user navigates to the widget area, you receive a delegate call and make a network call for the latest weather data. If the user wants more information, they can simply click on it and your application will open. Then everything will be in your hands.



Your option: you can always receive dynamic content when a user opens your app for a specific date and sets a notification for it. But this is not possible because the user may not receive updated data.

Push Notification: This may not be required in your case if you want to send dynamic data across your server to your application. This is always the best option.

+1


source


I created a function. In this case, you will call your function at a specific time whenever you want. I am creating a clock application, so I need to call a local notification when the user has created an alarm. And in the delegate method of Action Center, you can handle your response and call whatever method you want.

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
    }

}
}

      

And in AppDelegate methods: - you can access whenever a user clicks on your notification or when your notification is present. It is up to you what you want to do.

// MARK: - Notification delegates

@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])

    }
}

      

+1


source







All Articles