繁体   English   中英

iOS - 收到后阅读推送通知

[英]iOS - Read the push notification once Received

我有 4 个场景

当应用程序未启动时

鉴于该应用程序未启动或终止
当推送通知收到
并在不点击通知的情况下打开应用程序
然后应用程序应该捕获通知。

当应用程序在前台运行时

鉴于应用程序在前台运行
当推送通知收到
然后应用程序应该捕获通知。

当应用程序在后台运行时

鉴于应用程序在后台运行
当推送通知收到
并在不点击通知的情况下打开应用程序
然后应用程序应该捕获通知。

当应用程序未启动并清除通知时

鉴于应用程序未启动或终止
当推送通知收到
并且用户清除了通知
并打开了应用程序
然后应用程序应该捕获通知。

前 3 个场景适用于以下代码

最后一种情况不起作用当应用程序未启动并清除通知时

应用程序委托

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: { requests in
            for request in requests {
                self.setNotification(userInfo: request.request.content.userInfo as NSDictionary)
            }
        })
    }
    return true
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    setNotification(userInfo: userInfo as NSDictionary)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    setNotification(userInfo: userInfo as NSDictionary)
}

根据您的查询

当应用程序未启动并清除通知时

鉴于应用程序未启动或终止 当收到帖子通知并且用户清除通知并打开应用程序然后应用程序应捕获通知。

除非用户与该通知交互,否则这对于普通推送通知是不可能的。 您可能想尝试静默通知,这些未显示在 UI 中,但控件到达应用程序,您可以在代码中使用那里的数据/有效负载。

当用户从栏中清除通知时,无法获取该信息。 您还可以尝试在 API 内添加推送中发送的相同信息,并在用户打开应用程序后调用它。

此链接涉及所有相关细节。 根据您的实现,您可以尝试将两者结合使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM