swift - 如何根据推送通知的类型打开 Viewcontroller?
问题描述
我在我的应用程序中启用了推送通知,并且有 4 种类型的推送通知(类型 1、2、3、4)。如何让我的应用根据推送通知的类型打开不同的 ViewController?
我尝试搜索 stackoverflow 并找到了几个关于从推送通知中打开 VC 的线程,但不幸的是,根据推送通知的类型,我找不到任何关于打开 VC 的线程。
我是推送通知设置的新手,对此我一无所知,不胜感激。(这就是我无法包含任何代码的原因)
谢谢
编辑:通知的类型是 Int 1,2,3,4。
解决方案
您只需要找到顶视图控制器并根据您收到的推送通知类型推送相应的 vc。
在 Appdelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
handlePushNotification(userInfo as NSDictionary)
}
func handlePushNotification(_ userInfo: NSDictionary) {
if
let apsInfo = userInfo["aps"] as? NSDictionary,
let vcType = apsInfo["type"] as? String,
{
// TODO: Here you do string matching to find which VC you want to push
// Else you can send a local notification and read it where ever necessary
}
}
推荐阅读
- flutter - 如何用颤振计算列表字符串中的相同日期?
- java - 如何清除 Apache Tomcat 日志?
- directx - Windows 10 屏幕截图 DXGI 截图
- ios - 如何在 ios 目标 c 中创建嵌套滚动视图?
- python-3.x - 当我的电报机器人离线(非轮询)时,我可以接收发送到我的电报机器人的消息吗?
- regex - 如何删除大括号内的代码块?
- c# - 使用 Asp 标签导航到另一个 Html 页面 .net core mvc
- c# - 在初始化 aspnetcore 应用程序本身之前,如何在 aspnetcore 应用程序中设置我的日志记录服务
- node.js - 如何在 sequelize 中更新事务中的关联模型
- c - 如何用 Frama-C 证明 C stringCompare 函数的功能?