【问题标题】:Detect push notification selection when the app is at running state in iOS当应用程序在 iOS 中处于运行状态时检测推送通知选择
【发布时间】:2020-04-19 05:45:04
【问题描述】:

我正在使用didReceiveRemoteNotification 来检测应用通知。但是当应用程序处于运行状态时,它会自动触发。我需要在应用程序处于运行状态时检测到通知选择,而不是通过didReceiveRemoteNotification 自动检测通知。提前致谢

【问题讨论】:

    标签: ios swift push-notification apple-push-notifications push


    【解决方案1】:

    当应用在前台运行时,iOS 10+ 提供了自定义本地通知来处理此类问题。

    didFinishLaunchingWithOptions,添加委托

        UNUserNotificationCenter.current().delegate = self
    

    然后创建一个 appDelegate 扩展并添加它。

    @available(iOS 10, *)
    extension AppDelegate : UNUserNotificationCenterDelegate {
    
    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
    
    
    
        // Print full message.
        print(userInfo)
    
        // Change this to your preferred presentation option
        completionHandler([.alert,.sound])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                   didReceive response: UNNotificationResponse,
                                   withCompletionHandler completionHandler: @escaping () -> Void) {
           let userInfo = response.notification.request.content.userInfo
    
    
           // Print full message.
           print("tap on on forground app",userInfo)
    
           completionHandler()
       }
    
    }
    

    详情:

    Read This Tutorial

    【讨论】:

    • 感谢您的快速回复。我正在使用这个。但问题是,我无法在应用运行时处理通知选择。
    • @KaranAlangat 上面的答案是正确的 didReceive 响应应该调用,你检查了吗?
    • 是的。只有通知选择没有被检测到。
    • 我在我的应用程序中使用相同的代码。设置断点并检查函数是否被调用。
    • 非常感谢。这是代表的问题。 :)
    猜你喜欢
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多