提问人:Doogie 提问时间:8/28/2023 更新时间:8/28/2023 访问量:17
有没有办法只关闭选定的推送通知?(斯威夫特)
Is there a way to dismiss only the selected push notification? (Swift)
问:
例如,如果收到 4 个推送通知,并且用户选择了第 2 个推送通知进入应用程序,有没有办法将剩余的第 1 个、第 3 个和第 4 个推送通知留在通知中心?我的代码如下。
@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
private var isPushedBackground = true
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
HTTPCookieStorage.shared.cookieAcceptPolicy = HTTPCookie.AcceptPolicy.always
//FireBase Setting
FirebaseApp.configure()
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
}
extension AppDelegate: MessagingDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().setAPNSToken(deviceToken,
type: .sandbox)
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
guard let firebaseToken = fcmToken else {
return
}
UserDefaults.standard.setValue(firebaseToken, forKey: UserDefaultsKeys.fcmToken.keyString)
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.list, .badge, .sound, .banner])
self.isPushedBackground = false
}
//백그라운드 작동
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
guard let pushId = userInfo["pushId"] as? String else {
return
}
let pushAlarmInfo = Notification.PushAlarm.PushAlarmInfo(pushId: pushId,
isPushedBackground: self.isPushedBackground)
Notification.PushAlarm.postPushAlarm(pushAlarmInfo: pushAlarmInfo)
completionHandler()
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
completionHandler (.newData)
}
}
ChatGPT 为我提供了这样的代码,但它不起作用。代码如下。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let center = UNUserNotificationCenter.current()
center.getDeliveredNotifications { (notifications) in
// 푸시 알림을 확인 처리하고, 터치한 알림은 사라지지만 나머지는 유지
completionHandler(.newData)
}
}
答: 暂无答案
评论