Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  LocalNotificationManager.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 8/7/22.
6
//
7
 
8
import Foundation
9
import SwiftUI
10
 
11
class LocalNotificationManager  {
12
 
13
    init() {
14
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
15
                if granted == true && error == nil {
16
                    print("Notifications permitted")
17
                } else {
18
                    print("Notifications not permitted")
19
                }
20
            }
21
        }
22
 
23
    func sendNotification(title: String, subtitle: String?, body: String, launchIn: Double) {
24
 
25
            let content = UNMutableNotificationContent()
26
            content.title = title
27
            if let subtitle = subtitle {
28
                content.subtitle = subtitle
29
            }
30
            content.body = body
31
 
32
            let imageName = "logo"
33
            guard let imageURL = Bundle.main.url(forResource: imageName, withExtension: "png") else { return }
34
            let attachment = try! UNNotificationAttachment(identifier: imageName, url: imageURL, options: .none)
35
            content.attachments = [attachment]
36
 
37
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: launchIn, repeats: false)
38
            let request = UNNotificationRequest(identifier: Constants.NOTIFICATION_TRIGGER_NAME, content: content, trigger: trigger)
39
 
40
            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
41
        }
42
}
43
 
44