本地通知监听器未执行动作。
创始人
2024-11-30 03:30:20
0

在iOS中,可以使用UNUserNotificationCenterDelegate来监听和处理本地通知。当本地通知触发时,可以通过实现相应的代理方法来执行相应的动作。

以下是一个示例代码,在该代码中,我们创建了一个类遵循UNUserNotificationCenterDelegate协议,并实现了notificationCenter(_:didReceive:withCompletionHandler:)方法,当本地通知触发时,会执行该方法中的动作。

import UIKit
import UserNotifications

class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
    func notificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // 在这里执行你的动作
        print("本地通知被触发")
        
        // 完成处理
        completionHandler()
    }
}

// 在AppDelegate中设置通知代理
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // 注册通知
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
        // 处理授权结果
        if granted {
            print("通知授权成功")
        } else {
            print("通知授权失败")
        }
    }
    
    // 设置通知代理
    UNUserNotificationCenter.current().delegate = NotificationDelegate()
    
    return true
}

在以上代码中,我们创建了一个名为NotificationDelegate的类,并实现了notificationCenter(_:didReceive:withCompletionHandler:)方法。在这个方法中,你可以执行你想要的动作,例如弹出一个提示窗口、播放声音等。完成处理后,记得调用completionHandler()来通知系统已经处理完毕。

然后,在AppDelegate的application(_:didFinishLaunchingWithOptions:)方法中,我们设置了UNUserNotificationCenter的代理为我们刚刚创建的NotificationDelegate实例。

这样,当本地通知触发时,就会执行NotificationDelegate中的动作了。

相关内容

热门资讯

九分钟辅助!hhpoker作弊... 九分钟辅助!hhpoker作弊实战视频,wpk俱乐部是真的吗,指南教程(有挂猫腻)1、hhpoker...
第九分钟辅助!wepoker软... 第九分钟辅助!wepoker软件安装包,hh poker软件,手册教程(今日头条)一、wepoker...
七分钟辅助!wepoker插件... 七分钟辅助!wepoker插件辅助,约局吧德州真的存在透视吗,大纲教程(有挂猫腻)1、约局吧德州真的...
三分钟辅助!hh poker软... 三分钟辅助!hh poker软件,wepoker线上大神,课程教程(有挂详情)1、在wepoker线...
七分钟辅助!aapoker万能... 七分钟辅助!aapoker万能辅助器,佛手大菠萝有挂吗,策略教程(有挂教程)所有人都在同一条线上,像...
8分钟辅助!wepoker私人... 8分钟辅助!wepoker私人局透视插件,约局吧透视挂下载,窍门教程(有挂方法)1、让任何用户在无需...
第1分钟辅助!we-poker... 第1分钟辅助!we-poker正规吗,xpoker辅助神器,演示教程(有挂辅助)1、下载好xpoke...
两分钟辅助!wepoker有透... 两分钟辅助!wepoker有透视底牌吗,德扑圈透视挂,办法教程(有挂规律)暗藏猫腻,小编详细说明we...
第2分钟辅助!hhpoker真... 第2分钟辅助!hhpoker真的有透视吗,wepoker看底牌,法子教程(有挂猫腻)1、许多玩家不知...
四分钟辅助!hhpoker辅助... 四分钟辅助!hhpoker辅助软件,xpoker透视辅助,要领教程(果真有挂)1、超多福利:超高返利...