当用户使用 iOS Swift 点击推送通知时,在特定视图中打开应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33706455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open app in specific view when user taps on push notification with iOS Swift
提问by mechdon
My app allows remote push notifications to a user. How do I enable it to be opened in a specific view controller when the user taps on the push notification? I want the app to open and navigate to a specific view controller depending on the push notification received.
我的应用程序允许向用户远程推送通知。当用户点击推送通知时,如何让它在特定的视图控制器中打开?我希望应用程序根据收到的推送通知打开并导航到特定的视图控制器。
回答by kabiroberai
To do this you need to set an identifier
for each ViewController
that your app may be opened with, and then check the payload
in the launchOptions
argument of application:didFinishLaunchingWithOptions:
in your AppDelegate
Here are the steps to doing this:
为此,您需要为您的应用程序可能打开的identifier
每个设置一个ViewController
,然后检查您payload
的launchOptions
参数application:didFinishLaunchingWithOptions:
中的AppDelegate
以下是执行此操作的步骤:
In your
PFPush
, usesetData
to add a key to your payload with the identifier:notification.setData(["alert":"your notification string", "identifier":"firstController"])
Set the
identifier
on eachViewController
by selecting it and changing the following values
在您的
PFPush
, 中,使用setData
以下标识符将密钥添加到您的有效负载:notification.setData(["alert":"your notification string", "identifier":"firstController"])
通过选择它并更改以下值来设置
identifier
每个ViewController
- Make your Push Notification send the storyboard ID in its
payload
with the keyidentifier
- 让您的推送通知
payload
使用密钥发送故事板 IDidentifier
- Check for the ID in application:didFinishLaunchingWithOptions: by adding the following at the end of the function:
- 检查 application:didFinishLaunchingWithOptions: 中的 ID,方法是在函数末尾添加以下内容:
if let payload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary, identifier = payload["identifier"] as? String {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier(identifier)
window?.rootViewController = vc
}
回答by Satyam
In the AppDelegate, you will get a delegate callback "didFinishLoading" or "didReceivePushNotification" methods (based on your app is in background or foreground). In that method get the top most view controller's instance, then create the specific view controller that you want to show and present/push from top most view controller.
在 AppDelegate 中,您将获得委托回调“didFinishLoading”或“didReceivePushNotification”方法(基于您的应用程序是在后台还是前台)。在该方法中获取最顶层视图控制器的实例,然后创建您想要从最顶层视图控制器显示和呈现/推送的特定视图控制器。
回答by iOS Lifee
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)
{
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}