//
// UserNotificationManager.swift
// TestAppForIos
//
// Created by wangxuncai on 2021/10/16.
//
import Foundation
import UserNotifications
import CoreLocation
class UserNotificationManager{
static let instance = UserNotificationManager()
func requestAuthorization(){
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (success, error) in
if let error = error {
print("something goes wrong,\(error) ")
}else{
print("success")
}
}
}
func sendNotification(){
let content = UNMutableNotificationContent()
content.title = "简单备忘录"
content.body = "简单备忘录有一条待办事项即将到期"
content.sound = .default
//5秒之后触发通知
// let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
//17:05触发通知
// var date = DateComponents()
// date.hour = 17 /* 使用24小时制*/
// date.minute = 18
//
// let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
let coordinate = CLLocationCoordinate2D(
latitude: 50,
longitude: 60)
let region = CLCircularRegion(
center: coordinate,
radius: 200,
identifier: "")
region.notifyOnEntry = true
region.notifyOnExit = true
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
let request = UNNotificationRequest(identifier: "notification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
func removeNotification(){
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
}
// 使用方法
// ContentView.swift
// TestAppForIos
//
// Created by wangxuncai on 2021/10/15.
//
import SwiftUI
struct ContentView: View {
var body: some View {
Button {
UserNotificationManager.instance.requestAuthorization()
} label: {
Text("允许通知")
}
Button {
UserNotificationManager.instance.sendNotification()
} label: {
Text("发送通知")
}
//清除角标
.onAppear {
UIApplication.shared.applicationIconBadgeNumber = 0
}
}
}
苹果应用商店搜 王勋才 有我全部作品
网友评论