Skip to content

Instantly share code, notes, and snippets.

@kyungpyoda
Created November 30, 2021 00:40
Show Gist options
  • Save kyungpyoda/52faa6471d1ffa3da8947942245216c9 to your computer and use it in GitHub Desktop.
Save kyungpyoda/52faa6471d1ffa3da8947942245216c9 to your computer and use it in GitHub Desktop.
[iOS, Swift] To Implement Local Push Notification
//
// Notifier.swift
//
// Created by 홍경표 on 2021/11/29.
//
import Foundation
import UserNotifications
public class Notifier {
public class func reserve(
category: String? = nil,
title: String? = nil,
body: String,
fireDateTime: TimeInterval,
hour: Int?,
minute: Int? = 0,
second: Int? = 0,
userInfo: [AnyHashable: Any]? = nil,
notiID: String = UUID().uuidString
) {
let content = UNMutableNotificationContent()
if let category = category {
content.categoryIdentifier = category
}
if let title = title {
content.title = title
}
content.body = body
if let dic = userInfo {
content.userInfo = dic
}
let fireDate = Date(timeIntervalSince1970: fireDateTime)
var dateCompo = Calendar(identifier: .gregorian).dateComponents(
[.year, .month, .day, .hour, .minute, .second],
from: fireDate
)
dateCompo.setValue(hour, for: .hour)
dateCompo.setValue(minute, for: .minute)
dateCompo.setValue(second, for: .second)
// 현재 시간보다 과거인 경우 무시
guard (Calendar(identifier: .gregorian).date(from: dateCompo)?.timeIntervalSince1970 ?? .init()) > Date().timeIntervalSince1970 else { return }
let trigger = UNCalendarNotificationTrigger(dateMatching: dateCompo, repeats: false)
let request = UNNotificationRequest(identifier: notiID, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
if let error = error {
print("[error]", "Failed to add Push Noti:", error, error.localizedDescription)
} else {
print("[Info]", "Push(id: \(notiID)) Noti Reserved at:", "[\(dateCompo)]")
}
}
}
static func cancel(notiIDs: [String]) {
UNUserNotificationCenter.current()
.removePendingNotificationRequests(withIdentifiers: notiIDs)
}
static func cancelAll() {
UNUserNotificationCenter.current()
.removeAllPendingNotificationRequests()
}
}
// In AppDelegate.swift
extension AppDelegate: UNUserNotificationCenterDelegate {
/// Called when App is in the foreground.
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
if #available(iOS 14.0, *) {
completionHandler([.banner, .list, .alert, .badge, .sound])
} else {
completionHandler([.alert, .badge, .sound])
}
}
/// The method will be called on the delegate when the user responded to the notification
/// by opening the application, dismissing the notification or choosing a UNNotificationAction.
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
completionHandler()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment