【问题标题】:Hide remote notification at specific viewcontroller when app is in foreground in swift 3当应用程序在swift 3中处于前台时隐藏特定视图控制器的远程通知
【发布时间】:2018-07-17 12:53:29
【问题描述】:

我在我的聊天应用程序中使用 firebase 通知,并将其发送到我的设备。我的问题是如何在特定视图控制器上隐藏远程通知。当应用程序在前台时,我正在使用

UNUserNotificationCenter,将呈现委托方法

显示横幅,但是当我在 chatViewController 中时,我不想显示通知横幅、声音、警报等我想要的应用程序功能......对不起我的英语

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    let userInfo:[AnyHashable:Any] =  notification.request.content.userInfo
    print("\(userInfo)")

    completionHandler([.alert, .badge, .sound])
}

【问题讨论】:

  • 查看UNNotificationPresentationOptions的所有值。应该有一个.none(Objective-C 中的UNNotificationPresentationOptionNone)。当您决定不想显示它时,在completionHandler() 中使用它。但是,知道当前向用户呈现的是哪个 ViewController 是另一个问题(并且取决于您的应用架构)。
  • 所以在伪代码中:if showAlert {completionHandler([.alert, .badge, .sound]} else {completionHandler([.none])}

标签: ios swift3 push-notification swift4


【解决方案1】:

这对我们有用-

AppDelegate.m

// Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
    completionHandler(UNNotificationPresentationOptionNone);
}

【讨论】:

    【解决方案2】:

    传递空的completionHandler([])来隐藏远程通知。

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent 
                                notification: UNNotification, 
                                withCompletionHandler completionHandler: 
                                @escaping (UNNotificationPresentationOptions) -> 
               Void) {
    
    let userInfo:[AnyHashable:Any] =  notification.request.content.userInfo
    print("\(userInfo)")
    
    completionHandler([])
    }
    

    【讨论】:

      【解决方案3】:

      指定 UNNotificationPresentationOptionNone 将通知完全静音。

      UNNotificationPresentationOptionNone

      【讨论】:

        【解决方案4】:

        您可以使用以下方法找到当前视图控制器:

        extension UIViewController {
            var topViewController: UIViewController? {
                return self.topViewController(currentViewController: self)
            }
        
            private func topViewController(currentViewController: UIViewController) -> UIViewController {
                if let tabBarController = currentViewController as? UITabBarController,
                    let selectedViewController = tabBarController.selectedViewController {
                    return self.topViewController(currentViewController: selectedViewController)
                } else if let navigationController = currentViewController as? UINavigationController,
                    let visibleViewController = navigationController.visibleViewController {
                    return self.topViewController(currentViewController: visibleViewController)
               } else if let presentedViewController = currentViewController.presentedViewController {
                    return self.topViewController(currentViewController: presentedViewController)
               } else {
                    return currentViewController
                }
            }
        }
        

        然后,在func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 中添加以下代码:

        if self.window?.rootViewController?.topViewController is ChatViewController {
            completionHandler([])
        } else {
            completionHandler([.alert, .badge, .sound])
        }
        

        【讨论】:

        • 出现错误。UNNotificationPresentationOptions'没有成员'none'
        • 对不起,错误的属性。使用[] 而不是.none
        猜你喜欢
        • 2017-02-04
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-21
        • 2023-03-19
        相关资源
        最近更新 更多