Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. func enviarNotificacao(_ titulo:String, _ subtitulo:String, _ mensagem:String,
  2. _ identificador:String, _ tempo:TimeInterval) {
  3.  
  4. //Essa instancia de classe é necessária para criar o corpo da notificação
  5. let contexto = UNMutableNotificationContent()
  6.  
  7. //Criando corpo da notificação
  8. contexto.title = titulo
  9. contexto.subtitle = subtitulo
  10. contexto.body = mensagem
  11. contexto.sound = UNNotificationSound.default
  12. //Badge é a o alerta vermelho que fica no icone do aplicativo quando há
  13. //notificações e ela pode ser incrementada
  14. contexto.badge = 1
  15. contexto.categoryIdentifier = identificador
  16.  
  17. //--------------------------Linhas Adicionadas----------------------------
  18. //Criando os botões de ação
  19. let acaoDeSoneca = UNNotificationAction(identifier: "Soneca",
  20. title: "Soneca", options: [])
  21.  
  22. let acaoDeDesligar = UNNotificationAction(identifier: "Desligar",
  23. title: "Desligar",
  24. options: [.destructive])
  25.  
  26. let categoria = UNNotificationCategory(identifier: identificador,
  27. actions: [acaoDeSoneca, acaoDeDesligar],
  28. intentIdentifiers: [],
  29. options: [])
  30.  
  31. //Adicionando as ações ao nosso centro de notificações
  32. centroDeNotificacao.setNotificationCategories([categoria])
  33. //-------------------------------------------------------------------------
  34.  
  35. //Criando a requisição
  36. let gatilho = UNTimeIntervalNotificationTrigger(timeInterval: tempo,
  37. repeats: false)
  38. let requisicao = UNNotificationRequest(identifier: identificador,
  39. content: contexto, trigger: gatilho)
  40.  
  41.  
  42. //Adicionando a requisição ao nosso centro de notificações
  43. centroDeNotificacao.add(requisicao) { (error) in
  44. if let error = error {
  45. print("Deu ruim: \(error.localizedDescription)")
  46. }
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement