Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func enviarNotificacao(_ titulo:String, _ subtitulo:String, _ mensagem:String,
- _ identificador:String, _ tempo:TimeInterval) {
- //Essa instancia de classe é necessária para criar o corpo da notificação
- let contexto = UNMutableNotificationContent()
- //Criando corpo da notificação
- contexto.title = titulo
- contexto.subtitle = subtitulo
- contexto.body = mensagem
- contexto.sound = UNNotificationSound.default
- //Badge é a o alerta vermelho que fica no icone do aplicativo quando há
- //notificações e ela pode ser incrementada
- contexto.badge = 1
- contexto.categoryIdentifier = identificador
- //--------------------------Linhas Adicionadas----------------------------
- //Criando os botões de ação
- let acaoDeSoneca = UNNotificationAction(identifier: "Soneca",
- title: "Soneca", options: [])
- let acaoDeDesligar = UNNotificationAction(identifier: "Desligar",
- title: "Desligar",
- options: [.destructive])
- let categoria = UNNotificationCategory(identifier: identificador,
- actions: [acaoDeSoneca, acaoDeDesligar],
- intentIdentifiers: [],
- options: [])
- //Adicionando as ações ao nosso centro de notificações
- centroDeNotificacao.setNotificationCategories([categoria])
- //-------------------------------------------------------------------------
- //Criando a requisição
- let gatilho = UNTimeIntervalNotificationTrigger(timeInterval: tempo,
- repeats: false)
- let requisicao = UNNotificationRequest(identifier: identificador,
- content: contexto, trigger: gatilho)
- //Adicionando a requisição ao nosso centro de notificações
- centroDeNotificacao.add(requisicao) { (error) in
- if let error = error {
- print("Deu ruim: \(error.localizedDescription)")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement