IOS/objective-C : Direct users to specific screen of app on clicking on notification

标签 ios objective-c uilocalnotification

在 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 文件中使用 didReceiveLocalNotificationdidReceiveNotificationResponse 委托(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/

相关文章:

ios - 实例化——它有什么作用?

ios - 无法覆盖在另一个模块 swift 的类扩展中声明的打开方法

IOS取消本地通知

ios - 从另一个应用程序打开“设置”应用程序

ios - 使用 ReactiveCocoa 4 在 MVVM 中将信号从 View 传递到模型

ios - 在持久化 UIView 的同时推送 navigationController

objective-c - beginSheet modalForwindow : nil position?

ios - 根据子查询解析查询

iphone - iOS - UILocalNotification 不触发(在后台设置时)

ios - 保持 UNNotificationContent 几秒钟