用户通过触摸收到的推送通知打开应用程序

Posted

技术标签:

【中文标题】用户通过触摸收到的推送通知打开应用程序【英文标题】:User opens application by touching received push notification 【发布时间】:2017-05-11 13:14:40 【问题描述】:

在我的应用程序中,我有三个视图控制器,我们称它们为 MainVC、ObjectsVC 和 EventsVC(抱歉,如果您不喜欢这样的快捷方式)。 MainVC 是整个应用程序的初始 VC,其余的是 TableVC,其中包含带有一些对象的列表。 此应用支持带有“id”字段的推送通知: - id = 0 -> 应用正常启动 - id = 1 -> 应用以 ObjectsVC 开头 - id = 2 -> 应用以 EventsVC 开头 当用户点击传入的 PN 时,如何实现上面列出的转换?

【问题讨论】:

【参考方案1】:

一种简单的方法是返回初始的viewController,然后推送所需的。例如:

if id == 2 
    let eventsVc = EventsVC()
    navigationController?.popToRootViewController(animated: false)
    navigationController?.pushViewController(eventsVc, animated: false)

如果所需的viewController 已经在导航堆栈中,您应该检查它是否真的存在并且pop 到它。

这实际上取决于所需的导航行为。

【讨论】:

如果 MainVC 和 ObjectsVC 之间的视图少了怎么办?【参考方案2】:

尝试使用

(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;

您可以通过 notification.request.content.userInfo 获取您的 notificationId

【讨论】:

以上是关于用户通过触摸收到的推送通知打开应用程序的主要内容,如果未能解决你的问题,请参考以下文章

打开 PWA 时收到推送事件时不显示通知

从通知中导航到父活动

当您收到带有 onesignal 的推送通知时自动打开应用程序

收到通知并启用 Touch ID 时如何打开特定的 ViewController

尝试通过推送通知在 AppDelegate 中打开 SFSafariViewController

SwiftUI - 当用户打开推送通知时打开特定视图