一网通办ios
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

UserNotificationManager.swift 1.9 KiB

há 2 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // UserNotificationManager.swift
  3. // im-client-ios
  4. //
  5. // Created by 北京居家科技有限公司 on 2022/2/24.
  6. //
  7. import Foundation
  8. import UserNotifications
  9. import CoreLocation
  10. class UserNotificationManager{
  11. static let instance = UserNotificationManager()
  12. func requestAuthorization(){
  13. UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (success, error) in
  14. if let error = error {
  15. print("something goes wrong,\(error) ")
  16. }else{
  17. print("success")
  18. }
  19. }
  20. }
  21. func sendNotification(title: String!, body: String!){
  22. let content = UNMutableNotificationContent()
  23. content.title = title
  24. content.body = body
  25. content.sound = .default
  26. //5秒之后触发通知
  27. // let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
  28. //17:05触发通知
  29. // var date = DateComponents()
  30. // date.hour = 17 /* 使用24小时制*/
  31. // date.minute = 18
  32. //
  33. // let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
  34. let coordinate = CLLocationCoordinate2D(
  35. latitude: 50,
  36. longitude: 60)
  37. let region = CLCircularRegion(
  38. center: coordinate,
  39. radius: 200,
  40. identifier: "")
  41. region.notifyOnEntry = true
  42. region.notifyOnExit = true
  43. let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
  44. let request = UNNotificationRequest(identifier: "notification", content: content, trigger: trigger)
  45. UNUserNotificationCenter.current().add(request)
  46. }
  47. func removeNotification(){
  48. UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
  49. UNUserNotificationCenter.current().removeAllDeliveredNotifications()
  50. }
  51. }