在 appDelegate 的默认 - (void)applicationDidBecomeActive:(UIApplication *)application {
方法中,Apple 包括注释:
//This may also be a good place to direct someone
to a certain screen if the app was in background but they just clicked on a notification
我正尝试为本地通知执行此操作,但无法弄清楚如何将用户定向到特定屏幕。 The docs说你可以检查 URL 但即便如此,我不知道如何在应用程序中为屏幕指定 NSUrl。
这似乎是一个常见的用例,我们总是点击将我们带到应用程序特定部分的通知,但找不到任何好的资源。
提前感谢您的任何建议。
最佳答案
你必须在你的 AppDelegate.m
文件中使用 didReceiveLocalNotification
或 didReceiveNotificationResponse
委托(delegate)方法,基于你实现的通知
对于 UILocalNotification:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// Your redirection code goes here like shown below
MyViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
[self.window.rootViewController.navigationController pushViewController:controller animated:YES];
}
用于取消通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler {
// Your redirection code goes here like shown below
MyViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
[self.window.rootViewController.navigationController pushViewController:controller animated:YES];
}
有关UNNotification
或处理本地通知的更多引用,请参阅https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SchedulingandHandlingLocalNotifications.html
关于IOS/objective-C : Direct users to specific screen of app on clicking on notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50178886/