【问题标题】:How to clear the remote notification in your app?如何清除应用中的远程通知?
【发布时间】:2015-08-02 13:33:34
【问题描述】:

从 iPhone 屏幕顶部向下滑动时,有没有办法从通知横幅中清除远程通知。我尝试将徽章编号设置为零:

application.applicationIconBadgeNumber = 0 

在委托didFinishLaunchingWithOptionsdidReceiveRemoteNotification 中,但它没有清除通知。谢谢。

【问题讨论】:

    标签: ios swift remote-notifications


    【解决方案1】:

    您需要将 IconBadgeNumber 设置为 0 并取消当前通知。我从来没有快速做过,但我认为它的代码如下:

    application.applicationIconBadgeNumber = 0 
    application.cancelAllLocalNotifications()
    

    斯威夫特 5

    UNUserNotificationCenter.current().removeAllDeliveredNotifications()
    

    【讨论】:

    • 使用 cancelAllLocalNotifications?这是远程通知。感谢 cmets。
    • 抱歉,我对本地通知感到困惑,因为我通常同时使用两者。
    • 尝试@icaroNZ 建议的方法,它可以帮助从通知栏中删除通知。
    • 使用 applicationIconBadgeNumber 对我有用。谢谢。
    【解决方案2】:

    我必须先增加然后减少徽章计数才能使其工作:

    application.applicationIconBadgeNumber = 1
    application.applicationIconBadgeNumber = 0
    application.cancelAllLocalNotifications()
    

    【讨论】:

      【解决方案3】:

      斯威夫特 3

      AppDelegate.swift 文件下的didFinishLaunchingWithOptions 添加:

      application.applicationIconBadgeNumber = 0
      

      在您的应用启动时,这将移除 iOS 徽章(应用图标右上角的红色圆圈)。

      【讨论】:

        【解决方案4】:

        在 iOS 10 中,首先所有的解决方案都被贬值了

        'cancelAllLocalNotifications()' 在 iOS 10.0 中已弃用:使用 UserNotifications Framework 的 -[UNUserNotificationCenter removeAllPendingNotificationRequests]

        使用以下代码取消通知并重置徽章计数

        对于 iOS 10,Swift 3.0

        cancelAllLocalNotifications 从 iOS 10 中弃用。

        @available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]")
        open func cancelAllLocalNotifications()
        

        您必须添加此导入语句,

        import UserNotifications
        

        获取通知中心。并执行如下操作

        application.applicationIconBadgeNumber = 0 // For Clear Badge Counts
        let center = UNUserNotificationCenter.current()
        center.removeAllDeliveredNotifications() // To remove all delivered notifications
        center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
        

        如果要删除单个或多个特定通知,可以通过以下方法实现。

        center.removeDeliveredNotifications(withIdentifiers: ["your notification identifier"])
        

        希望对您有所帮助..!!

        【讨论】:

          【解决方案5】:

          任何寻找 swift 4 及以上代码的人

          application.applicationIconBadgeNumber = 0
          UNUserNotificationCenter.current().removeAllDeliveredNotifications()
          

          【讨论】:

            【解决方案6】:

            2019 年 11 月,以下是我使用 Swift 4 的有效解决方案。首先,您必须检查设备版本以清除所有通知,但无需检查以重置徽章计数。

            override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?  ) -> Bool {
            
              //--------
            
              application.applicationIconBadgeNumber = 0
              if #available(iOS 10.0, *) {        
                 let center = UNUserNotificationCenter.current() 
                 center.removeAllDeliveredNotifications() 
                 center.removeAllPendingNotificationRequests()    
              } else {        
                 application.cancelAllLocalNotifications()    
              }        
            
              //------
            }
            

            【讨论】:

              【解决方案7】:

              这适用于应用被用户强行终止的情况:

              当您想通过推送通知向用户发送生日提醒通知时,首先发送非零徽章,例如:

               {
                "aps": {
                  "alert": {
                    "title": "Hey! Urgent Reminder",
                    "body": "Do not forget my wife SURPRISE BIRTHDAY PARTY"
                  },
                  "badge": 1
                }
              } 
              

              之后,当不需要在设备中显示通知时,您可以发送带有零标记的静默通知,即使应用强制终止也会清除标记和通知strong> 由用户调用,但不会调用 didReceiveRemoteNotification,因为应用程序已终止。 静默推送通知的有效负载:

               {
                 "aps" : {
                    "content-available" : 1,
                      "badge" : 0,
                      "Priority" : 10
                 }
              }
              

              发送该有效负载后,将自动清除徽章并从通知中心删除推送通知。

              不是。如果在发送静默通知之前徽章为零,则不会清除通知。

              https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

              【讨论】:

                【解决方案8】:

                Swift 4 和 5

                import UserNotifications
                
                ...
                ...
                ...
                
                UNUserNotificationCenter.current().removeAllDeliveredNotifications()
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2012-01-30
                  • 2021-09-26
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-09-14
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多