Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Autoría | Ultima modificación | Ver Log |

//
//  LocalNotificationManager.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 8/7/22.
//

import Foundation
import SwiftUI

class LocalNotificationManager  {

    init() {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
                if granted == true && error == nil {
                    print("Notifications permitted")
                } else {
                    print("Notifications not permitted")
                }
            }
        }
    
    func sendNotification(title: String, subtitle: String?, body: String, launchIn: Double) {
            
            let content = UNMutableNotificationContent()
            content.title = title
            if let subtitle = subtitle {
                content.subtitle = subtitle
            }
            content.body = body
               
            let imageName = "logo"
            guard let imageURL = Bundle.main.url(forResource: imageName, withExtension: "png") else { return }
            let attachment = try! UNNotificationAttachment(identifier: imageName, url: imageURL, options: .none)
            content.attachments = [attachment]
        
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: launchIn, repeats: false)
            let request = UNNotificationRequest(identifier: Constants.NOTIFICATION_TRIGGER_NAME, content: content, trigger: trigger)
        
            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        }
}