【问题标题】:How to clear push notifications programmatically-iOS如何以编程方式清除推送通知-iOS
【发布时间】:2013-03-07 16:29:59
【问题描述】:

用户阅读后,我需要从通知中心清除推送通知。我知道有 cancelAllLocalNotifications 方法,但是如何清除所有远程通知? 作为补充,我希望有以下功能,如果用户在通知中心有 5 条消息,我们只有在用户阅读所有通知后才会清除所有通知。任何想法如何实现这些?提前感谢您的帮助。

【问题讨论】:

    标签: ios push-notification


    【解决方案1】:

    如果你想快速清除通知

    import UserNotifications
    
    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.removeAllPendingNotificationRequests() // To remove all    pending notifications which are not delivered yet but scheduled.
        center.removeAllDeliveredNotifications() // To remove all delivered notifications
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用setApplicationIconBadgeNumber 清除它们,例如:

      [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
      

      【讨论】:

      • 这似乎不会从通知中心删除通知,它只是删除了徽章编号。
      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多