如何在单击本地通知时获取特定的视图控制器?..我尝试了所有可能的方法来实现但不能

Posted

技术标签:

【中文标题】如何在单击本地通知时获取特定的视图控制器?..我尝试了所有可能的方法来实现但不能【英文标题】:How to get specific view controller on click of local Notification?..I tried all possible way to achieve but couldn't 【发布时间】:2018-03-26 10:36:22 【问题描述】:

我在第一个具有按钮的控制器上有两个视图控制器,单击它会触发本地通知,但我想在单击本地通知时打开第二个视图控制器。已浏览所有相关帖子,但无法得到解决方案。

我的代码是: 在第一个视图控制器视图中加载:

 isGrantedNotificationAccess = false;
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNAuthorizationOptions options =  UNAuthorizationOptionSound + UNAuthorizationOptionAlert;
    [center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) 
        isGrantedNotificationAccess = granted;
    ];

仅在第一个视图控制器中的按钮操作:

 - (IBAction)buttonAction:(id)sender 
    if (isGrantedNotificationAccess) 
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        UNMutableNotificationContent *muContent = [[UNMutableNotificationContent alloc] init];
        muContent.title = @"Keshav Title";
        muContent.subtitle = @"He is an ios Developer";
        muContent.body = @"He is a body";
        muContent.sound = [UNNotificationSound defaultSound];
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UYLocalNotification" content:muContent               trigger: trigger];

        [center addNotificationRequest:request withCompletionHandler:nil];
    
 

在 appdelegate.m 中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate  = self;
    return  YES;



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

    UNNotificationPresentationOptions present = UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound ;
    completionHandler(present);


-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

    if(application.applicationState == UIApplicationStateActive) 
        //app is currently active, can update badges count here
     else if(application.applicationState == UIApplicationStateBackground)
        //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        UIViewController *viewController =  [storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];
     else if(application.applicationState == UIApplicationStateInactive)
        //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
    

【问题讨论】:

请出示您的代码 【参考方案1】:

AppDelegate

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       didReceiveNotificationResponse:(UNNotificationResponse *)response
       withCompletionHandler:(void (^)(void))completionHandler 



      UINavigationController *nvc = (UINavigationController *)self.window.rootViewController;

      // Currently visible ViewController
      UIViewController *vc = nvc.visibleViewController;

      UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main"
                                             bundle:NULL];
      // YourViewController to Push in Your case SecondViewController
      yourViewController *myVc= [sb instantiateViewControllerWithIdentifier:@"yourViewController"];

      [navigationController pushViewController:myVc animated:true];

您可以通过response.actionIdentifier查看特定通知

在您的情况下使用response.notification.request.content.categoryIdentifier 你检查特定的通知

设置catagoryIdentifier

muContent.categoryIdentifier = @"Your_Identifier";

【讨论】:

UILocalNotification 已弃用。 谢谢,它对我有用。如果我有多个视图控制器和多个本地通知,并且我想在单击不同通知时打开不同的控制器。我怎样才能做到这一点? 谢谢你,实现了同样的目标。@vp2699

以上是关于如何在单击本地通知时获取特定的视图控制器?..我尝试了所有可能的方法来实现但不能的主要内容,如果未能解决你的问题,请参考以下文章

IOS 7 通过单击推送通知查看特定视图控制器

在 iPhone 通知中心单击消息时如何转到特定视图?

iOS 通过本地通知显示特定的视图控制器

带有 Residemenu 的 UILocalNotification

在 ios 中获取活动的本地通知警报类型或本地通知警报视图按钮单击事件

iOS本地通知打开特定选项卡?