7

Scheduling Local Notifications (Swift 5)

 3 years ago
source link: https://blog.usejournal.com/scheduling-local-notifications-swift-5-57d517476b8d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Scheduling Local Notifications (Swift 5)

Image for post
Image for post
Photo by Jamie Street on Unsplash

Difference between Local and Push Notification

There are two types of notifications in iOS Framework. Remote notifications (commonly called push notifications) are sent from a server or remote. Local Notifications as we are going to talk about in this blog is handled by UserNotificationsFramework.Let’s look at the documentation

You can generate notifications locally from your app or remotely from a server that you manage. For local notifications, the app creates the notification content and specifies a condition, like a time or location, that triggers the delivery of the notification.

Let’s imagine we are making a treasure hunt mobile game.User gathers in-game treasure box after playing some time.

Let’s define our TreasureBox

TreasureBox model contains 4 fields and functions.

id → Unique for identification (we will use in User Notification later)

name → Name of the treasure box

duration → Integer for treasure box total seconds before opens

reward → Reward of treasure box

Let’s define our Player

The Player model contains 3 fields.

Want to read this story later? Save it in Journal.

userName → Player username

userHasAuthorizedNotification → User granted notification permission

availableTreasures → array of players available TreasureBox

Player.swift

After Creating our model classes let’s move to our logic part. First, we create our ViewController.swift and define our Player model global.

Whenever an item of treasure box added to the player’s availableTreasures, our didSet function observing player in the beginning triggers and calls scheduleTreasureLocalNotification().

Then we have to move on to helper methods

Notifications can take several forms, but the most common thing to do is to request permission to view notices, badges, and sounds, which does not mean that we need to use all of them at the same time, but we can be selective later on by requesting permission up front.

It will show the user a prompt when we tell iOS what kinds of updates we want, so they have the final say on what our app will do. A closure we have will be called when they make their decision, and inform us whether or not the request was successful.

createDummyTreasureBox():

Creates TreasureBox by given values and returns. Also generates UUID for id.

registerForRemoteNotification():

Request notification permission & register for notification.Returns completion of notification authentication state as boolean. (If the user granted notification as true)

If permission is given by the user, then we are all clear about starting to schedule alerts.

. The notification content is what should be seen, and a title, subtitle, sound, image, and so on can be a title.

. The trigger decides when to view the message, which can be several seconds from now a future date and time, or a place.

. The request blends content and cause, but also adds a unique identifier so that you can later modify or delete relevant warnings.

Finally Creating our use case on Treasure Hunt

  1. Create our player model to simulate our user action login. Because initially, the player hasn’t allowed for notifications userHasAuthorizedNotification as false and because it has no available treasures availableTreasures as []
//Lets create our player (Pre defined fields entered static)
//Initially user has no available treasureself.player = Player(userName: "TreasureHunter",
userHasAuthorizedNotification: false,
availableTreasures: [])

2. We request a player for granting notifications.We assign the boolean value to our player userHasAuthorizedNotification

//Lets ask for notification permissionregisterForRemoteNotification(completion: { (permissionGranted) inself.player.userHasAuthorizedNotification = permissionGranted})

3. Finally we create treasures and assign to the player’s availableTreasures collection.

//Then create our Treasureslet goldTreasureBox = createDummyTreasureBox(name: "Gold Treasure Box", duration: 10, reward: "20 Gold")let expTreasureBox = createDummyTreasureBox(name: "Exp Treasure Box",duration: 20, reward:"100 Exp")//User gathers treasuresplayer.availableTreasures.append(goldTreasureBox)
player.availableTreasures.append(expTreasureBox)

More from Journal

There are many Black creators doing incredible work in Tech. This collection of resources shines a light on some of us:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK