Swift Sprite-Kit添加通知:如何实现定时召回与完成提醒?
简单实现SpriteKit游戏的本地通知方案
嘿,作为SpriteKit新手,咱们直接用iOS自带的UserNotifications框架搞定这两个需求,完全不用复杂第三方库,代码直白好上手,直接上干货!
一、用户停止游玩2天后弹出通知
这个需求核心是记录最后游玩时间,每次启动游戏时重新安排2天后的通知,同时取消旧通知避免重复。
步骤&代码实现
先请求通知权限(必须第一步,不然通知弹不出来)
可以在GameViewController的viewDidLoad里调用这个函数:import UserNotifications func requestNotificationPermission() { UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in if let error = error { print("请求通知权限失败:\(error.localizedDescription)") } else if granted { print("通知权限已获取") } else { print("用户拒绝了通知权限") } } }记录最后游玩时间(存在UserDefaults里,简单方便)
每次游戏启动/用户开始玩的时候调用:func updateLastPlayTime() { UserDefaults.standard.set(Date(), forKey: "LastPlayTime") }安排2天后的提醒通知
每次启动游戏时,先取消旧通知再安排新的:func scheduleReturnNotification() { // 取消之前的返回提醒通知 UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["ReturnToGame"]) // 计算2天后的时间 guard let twoDaysLater = Calendar.current.date(byAdding: .day, value: 2, to: Date()) else { print("计算时间失败") return } // 创建通知内容 let content = UNMutableNotificationContent() content.title = "回来继续战斗!" content.body = "你的俯视角射击游戏还在等你呢😎" content.sound = .default // 创建触发条件 let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: twoDaysLater), repeats: false) // 创建并添加通知请求 let request = UNNotificationRequest(identifier: "ReturnToGame", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) { error in if let error = error { print("安排返回通知失败:\(error.localizedDescription)") } else { print("已安排2天后的返回提醒") } } }启动时串联逻辑
在GameViewController的viewDidLoad里整合:override func viewDidLoad() { super.viewDidLoad() requestNotificationPermission() updateLastPlayTime() scheduleReturnNotification() // 你的SpriteKit场景初始化代码... }
二、升级完成时弹出通知
这个需求是升级开始时,根据时长安排对应时间的通知,中途取消升级就移除通知。
步骤&代码实现
安排升级完成通知的函数
假设你有upgradeDuration变量代表升级需要的秒数:func scheduleUpgradeCompleteNotification(upgradeName: String, durationInSeconds: TimeInterval) { // 取消之前的升级通知(如果存在) UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["UpgradeComplete"]) // 创建通知内容 let content = UNMutableNotificationContent() content.title = "升级完成!" content.body = "你的\(upgradeName)已经升级完毕,快来看看吧!" content.sound = .default // 用时间间隔触发,比日历更简单 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: durationInSeconds, repeats: false) // 创建并添加通知请求 let request = UNNotificationRequest(identifier: "UpgradeComplete", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) { error in if let error = error { print("安排升级通知失败:\(error.localizedDescription)") } else { print("已安排升级完成提醒") } } }取消升级通知的函数(用户取消升级时调用)
func cancelUpgradeNotification() { UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["UpgradeComplete"]) print("已取消升级完成通知") }
使用示例
- 开始升级时调用:
// 比如升级“突击步枪”需要2小时(7200秒) scheduleUpgradeCompleteNotification(upgradeName: "突击步枪", durationInSeconds: 7200) - 取消升级时调用:
cancelUpgradeNotification()
新手友好提醒
- 测试时可以把时间改短,比如把2天改成1分钟,升级时长改成10秒,方便快速验证;
- 一定要确保用户允许通知权限,不然所有通知都弹不出来;
- 这些代码可以放在
GameViewController里,也可以封装成工具类,方便在SpriteKit场景里调用。
内容的提问来源于stack exchange,提问作者Mini Official
相关产品推荐
相关产品推荐

