[Swift] How to send a notification

Check if you allow notifications

Before issuing a notification, you need to confirm whether you allow the notification.

python


//Get notification permission
UNUserNotificationCenter.current().requestAuthorization(
     options: [.alert, .sound, .badge]){
     (granted, _) in
     if granted{
          UNUserNotificationCenter.current().delegate = self
     }
}

Send a notification after the specified time has passed

○ If you want to execute it after a second, you need to create a notification with ʻUNTimeIntervalNotificationTrigger (timeInterval :, repeats :)`.

python


let content = UNMutableNotificationContent()
content.sound = UNNotificationSound.default
content.title = "title"
content.subtitle = "subtitle"
content.body = "Contents"

//Execute after specified time
let timer = 10

//Create a notification request
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timer), repeats: false)
let identifier = NSUUID().uuidString
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

//Register notification request
UNUserNotificationCenter.current().add(request){ (error : Error?) in
     if let error = error {
          print(error.localizedDescription)
     }
}

Send a notification when the specified time comes

If you want it to run at the specified time, you need to use ʻUNCalendarNotificationTrigger (dateMatching :, repeats :) `to create a notification.

python


let content = UNMutableNotificationContent()
content.sound = UNNotificationSound.default
content.title = "title"
content.subtitle = "subtitle"
content.body = "Contents"
                
//Specify notification time
let date = Date()
let newDate = Date(timeInterval: 60, since: date)
let component = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: newDate)
                
//Make a request
let trigger = UNCalendarNotificationTrigger(dateMatching: component, repeats: false)
let identifier = NSUUID().uuidString
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
                
//Register notification request
UNUserNotificationCenter.current().add(request){ (error : Error?) in
     if let error = error {
          print(error.localizedDescription)
     }
}

Recommended Posts

[Swift] How to send a notification
[Swift5] How to create a splash screen
How to leave a comment
[Swift] How to use UserDefaults
How to use Swift UIScrollView
How to insert a video
How to create a method
How to add columns to a table
Send a pull request to GitHub
[Swift] Event notification pattern
[Swift] Loop pattern matching by case-let
[Swift] How to send a notification
How to make a Java container
How to sign a Minecraft MOD
How to make a JDBC driver
[Java] How to create a folder
[Swift] How to use SwiftLint (cocoapods)
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
[Swift] How to replace multiple strings
How to make a Java array
[Swift5] How to communicate from ViewController to Model and pass a value
[Xcode] How to add a README.md file
How to sort a List using Comparator
How to make a Java calendar Summary
[swift5] How to specify color in hexadecimal
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
[Swift] Converts a UInt64 type integer to [UInt8]
[Basic] How to write a Dockerfile Self-learning ②
How to insert a video in Rails
How to add a new hash / array
[Introduction to Java] How to write a Java program
[Swift] How to fix Label in UIPickerView
How to create a Maven repository for 2020
[Swift] How to use Tab Bar Controller
How to make a Discord bot (Java)
[Swift] How to implement the countdown function
Java: How to send values from Servlet to Servlet
How to print a Java Word document
[Swift5] How to implement standby screen using'PKHUD'
[rails] How to create a partial template
[Swift5] How to implement animation using "lottie-ios"
How to publish a library in jCenter
How to overwrite Firebase data in Swift
[Swift] How to use one option alert
[SpringBoot] How to write a controller test
How to send push notifications on AWS
How to return a value from Model to Controller using the [Swift5] protocol
How to deploy
How to create a database for H2 Database anywhere
How to deploy a container on AWS Lambda
[Rails] How to create a graph using lazy_high_charts
[Android] How to convert a character string to resourceId
How to get a heapdump from a Docker container
How to display a web page in Java
[Swift] How to implement the LINE login function
How to delete a controller etc. using a command
Note how to use Swift super basic TableView
Convert a string to a character-by-character array with swift
[swift5] How to implement the Twitter share function
How to add sound in the app (swift)