Use local notifications to get the user’s attention. This type of notification managed locally.
And in the local notification no need for server-side help for invoking. Similarly, this will work like push notification
Local notification is a text-based message at top of the application.
we are using the UserNotifications framework to implement the local notification.
Therefore We will follow 2 steps to achieve local notifications.
In addition we define the user notification object creation.
1 – Request for user authorization
1 2 3 4 5 6 7 8 9 10 |
let center = UNUserNotificationCenter.current() func requestForLocalNotifications() { center.delegate = self center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in if error != nil { // Something went wrong } } } |
1 2 3 4 5 6 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { requestForLocalNotifications() return true } |
First, we will create the object of the UNUser notification center. After That we are using above function for user authorization.
And after this user allows for notification. Call the requestForLocalNotifications in app delegate didFinishlaunch function.
And this function executes first when the app load.
2 – Create local notification
we will invoke the local notification on button action.
To create the local notification we need to take care of three things.
1-> Content
Content used for defining which data will show with your notification
2-> Trigger
Trigger used for notification execution. and this defines when notification will triggered.
3-> Request
Request to combine the trigger and content for the notification request.
In other words this helps to fire the request.
1 2 3 |
@objc func cartNotificationAct(_ sender: UITapGestureRecognizer) { localCartNotification() } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
func localCartNotification() { let content = UNMutableNotificationContent() content.categoryIdentifier = "Cart notification" content.title = "Your Cart Misses You!" content.body = "You have products waiting in your cart to checkout. Running out of stock! Hurry!" content.badge = 0; let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 7200, repeats: false); let uuidString = UUID().uuidString let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger) center.add(request) { (error) in // for error } let likeaction = UNNotificationAction.init(identifier: "View", title: "View", options: UNNotificationActionOptions.foreground) let deleteaction = UNNotificationAction.init(identifier: "Delete", title: "Delete", options: .destructive) let category = UNNotificationCategory.init(identifier: content.categoryIdentifier, actions: [likeaction,deleteaction], intentIdentifiers: [], options: []); center.setNotificationCategories([category]) center.add(request) { (error) in // for error } } |
The above Code will fire the notification 2 hours but repeated notification restricted.
Conclusion
Thanks for reading this blog, You can get other blogs from here.
But This will help you to create the local notification and if you feel any issue or suggestion then leave a comment.