美文网首页
swiftUI 发送本地通知(UNUserNotificatio

swiftUI 发送本地通知(UNUserNotificatio

作者: 王勋才 | 来源:发表于2021-10-16 17:38 被阅读0次
//
//  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
        }

    }
}



苹果应用商店搜 王勋才 有我全部作品

相关文章

  • swiftUI 发送本地通知(UNUserNotificatio

  • SwiftUI:本地通知

    iOS具有一个名为UserNotifications的框架,该框架几乎可以完全满足您的期望:让我们为用户创建可以在...

  • UNUserNotificationCenter的基本使用

    引入头文件 注册通知 检查通知权限 发送本地通知 5.发送本地通知一定要实现以下代理方法 通知代理方法

  • 本地通知

    接收本地消息 其他 发送通知

  • 本地推送通知的 基本使用

    本地推送通知的 基本使用: 1. 发送通知。 2.取消通知。 3.获取“当前已经制定好的 需要发送的 那些通知”

  • ios发送本地通知

  • iOS 发送本地推送通知

    App在后台的情况希望通知用户看到信息时使用本地推送通知实现。1.导入系统框架 2.注册本地通知样式 3.清除之前...

  • ios10前后推送基本使用

    一、推送的分类 本地推送通知“本地”可以理解为”不联网”;即使没有网络情况下,也可以推送通知消息通知发送方: 开发...

  • 推送通知

    推送通知的分类 本地推送通知本地推送通知可以理解为不联网,即使没有网络也可以推送通知通知发送方:开发人员负责在AP...

  • iOS 本地消息通知

    本地消息通知的流程 1.注册 通过调用requestAuthorization这个方法,通知中心会向用户发送通知许...

网友评论

      本文标题:swiftUI 发送本地通知(UNUserNotificatio

      本文链接:https://www.haomeiwen.com/subject/xepboltx.html