首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的didRecive和willPresent不能工作(动作按钮什么都不做)?

为什么我的didRecive和willPresent不能工作(动作按钮什么都不做)?
EN

Stack Overflow用户
提问于 2022-05-25 07:59:24
回答 2查看 58关注 0票数 1

我想发出一份可起诉的通知。只有当我的应用程序关闭时,它才会出现。当我按下动作按钮什么都没发生

我的通知班

代码语言:javascript
复制
class Notfication : NSObject, UNUserNotificationCenterDelegate {

func request(answer : rope, RopesBas : RopesBase){
    let taskCategory = UNNotificationCategory(identifier:  "task", actions: [UNNotificationAction(identifier: "done", title: "Done")], intentIdentifiers: [])
    UNUserNotificationCenter.current().setNotificationCategories([taskCategory])
    print("0 stage")
    
    let content = UNMutableNotificationContent()
    content.title = answer.name
    content.sound =
    UNNotificationSound.default
    content.categoryIdentifier = "task"
    content.userInfo=["name" : answer.name]
    print("1 stage")
    
    // show this notification five seconds from now
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    print("2 stage")
    
    //UNUserNotificationCenter.current().delegate = self
    // choose a random identifier
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
    print("3 stage")
    
    // add our notification request
    UNUserNotificationCenter.current().add(request){ (error : Error?) in
        if let theError = error {
            print(theError.localizedDescription)
        }
    }
    print("4 stage")
    print(UNUserNotificationCenter.current().delegate)
    print("ok")
}


func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("5 stage")
let userInfo = response.notification.request.content.userInfo
let id = "dad"
print("6 stage")
print(response.actionIdentifier)
switch response.actionIdentifier {
case "done":
    base.ropes.removeAll(where: {$0.name == id})
    break
case UNNotificationDefaultActionIdentifier,
UNNotificationDismissActionIdentifier:
    
    break
default :
    break
}
    print(base.ropes.count)
    completionHandler()
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("q")
    completionHandler([.badge,.banner,.sound,.list])
}
}

我在工作表视图中声明这个类

代码语言:javascript
复制
struct Adding: View {
private let publisher = Notfication()

当我按下工作表视图中的按钮时,使用它。

代码语言:javascript
复制
Button(action: {
                RopesDB.ropes.append(rope(name: answer.answer))
                publisher.request(answer: RopesDB.ropes.last!, RopesBas: RopesDB)
                dismiss()
            }, label: {
                HStack{
                    Spacer()
                    Text(answer.answer)
                    Spacer()
                }

我试着把代表

代码语言:javascript
复制
class AppDelegate: NSObject, UIApplicationDelegate{
internal func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    let center = UNUserNotificationCenter.current()
    let options : UNAuthorizationOptions = [.alert, .badge, .sound]
    center.requestAuthorization(options: options) { granted, error in
        if granted{
            UNUserNotificationCenter.current().delegate = Notfication()
        }
            if let error = error {
                print(error.localizedDescription)
            }
    }
    return true
}
//

}并在课堂上评论我的错误?伊德斯威夫特游乐场(iPad 9,iOS 15.5)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-01 14:19:40

我发现这个东西不能在操场上工作,我想

票数 0
EN

Stack Overflow用户

发布于 2022-05-25 08:12:18

苹果在其文档中指出,只有在应用程序处于后台时才会显示通知。当应用程序已经处于前台时,通知将直接传递到应用程序:https://developer.apple.com/documentation/usernotifications/handling_notifications_and_notification-related_actions中。

您需要在委托的userNotificationCenter(_:willPresent:withCompletionHandler:)方法中接收通知,在那里您的应用程序可以决定实际如何处理它。将用户抛到某个视图,显示一个模态等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72373932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档