You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS开发:用户订阅APP后如何移除引导订阅的通知提醒

解决方案

实现逻辑说明

  • 新增订阅状态判断入口,可直接对接你自身的订阅状态管理逻辑
  • 给引导类通知添加固定标识前缀,方便精准筛选、移除对应通知,避免误删其他正常业务通知
  • 实现通知弹出拦截代理方法,用户已订阅时直接拦截引导通知的展示
  • 新增订阅成功后的通知清理方法,主动移除所有待发送的引导通知

完整修改后代码

import Foundation
import UserNotifications

class Notifications: NSObject, UNUserNotificationCenterDelegate {
    // 引导订阅通知固定标识前缀
    private let subscriptionGuideNotificationIDPrefix = "subscription_guide_"
    
    // 订阅状态判断,可替换为你实际的订阅状态读取逻辑(如从自有订阅管理类获取)
    private var isUserSubscribed: Bool {
        UserDefaults.standard.bool(forKey: "user_has_subscribed")
    }
    
    override init() {
        super.init()
        // 绑定通知中心代理,否则拦截方法不会触发
        UNUserNotificationCenter.current().delegate = self
    }
    
    func notificationRequest() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
            if success {
                print("All set!")
            } else if let error = error {
                print(error.localizedDescription)
            }
        }
    }
    
    func addNotification() {
        // 已订阅用户直接不添加引导通知
        guard !isUserSubscribed else { return }
        
        print("foi")
        let content = UNMutableNotificationContent()
        content.title = "WARNING: Safari at risk! ⚠️"
        content.subtitle = "Tap here to secure your device!"
        content.sound = UNNotificationSound.default

        // show this notification 3 days from now 259200
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 259200, repeats: true)

        // 标识增加固定前缀,方便后续筛选
        let request = UNNotificationRequest(identifier: "\(subscriptionGuideNotificationIDPrefix)\(UUID().uuidString)", content: content, trigger: trigger)

        // add our notification request
        UNUserNotificationCenter.current().add(request)
    }
    
    func clearNotification() {
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    }
    
    // 仅清理引导订阅类的待发送通知
    func clearSubscriptionGuideNotifications() {
        UNUserNotificationCenter.current().getPendingNotificationRequests { [weak self] requests in
            guard let self = self else { return }
            let guideNotificationIDs = requests.filter {
                $0.identifier.hasPrefix(self.subscriptionGuideNotificationIDPrefix)
            }.map(\.identifier)
            UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: guideNotificationIDs)
        }
    }
    
    // 通知弹出拦截代理方法
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // 若为引导订阅通知且用户已订阅,直接不展示
        if notification.request.identifier.hasPrefix(subscriptionGuideNotificationIDPrefix), isUserSubscribed {
            completionHandler([])
            return
        }
        // 其他通知正常展示
        completionHandler([.alert, .sound, .badge])
    }
}

使用方法

在你的订阅业务流程中,用户成功完成订阅后执行两步操作即可:

  1. 存储订阅标记:UserDefaults.standard.set(true, forKey: "user_has_subscribed")
  2. 调用实例方法清空待发送的引导通知:yourNotificationInstance.clearSubscriptionGuideNotifications()

内容的提问来源于stack exchange,提问作者flutterloop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 19:45:04