ios - 如何在收到本地通知数据后创建传递给使用 objective-c 的主视图 Controller ?

标签 ios objective-c localnotification

我已经使用 Objective C 为 iPhone 应用程序创建了本地通知流程。我想将 didReceiveLocalNotification 之后的数据传递给主视图 Controller 。

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {


    if (app.applicationState == UIApplicationStateInactive ) {
        NSLog(@"app not running");

       // I need to pass the data to main view controller 


    }else if(app.applicationState == UIApplicationStateActive )  {
        NSLog(@"app running");


        // I need to pass the data to main view controller

    }

    NSLog(@"Recieved Notification %@",notif);  // Handle the notificaton when the app is running 

}

最佳答案

你可以使用 NSNotificationCenter 来传递数据。

MainViewController.m

viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifMainController:) name:@"notifMainController" object:nil];

-(void)notifMainController:(NSNotification *)notif
{
    NSLog(@"%@",notif.object);
}

在 AppDelegate 中

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif 
{

 NSLog(@"Recieved Notification %@",notif);  // Handle the notificaton when the app is running 

if (app.applicationState == UIApplicationStateInactive ) 
{
    NSLog(@"app not running");
   // I need to pass the data to main view controller 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            MainViewController *obj = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
            [self.navC pushViewController:obj animated:NO];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"];
        });

}
else if(app.applicationState == UIApplicationStateActive )  
{
    NSLog(@"app running");
    // I need to pass the data to main view controller
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"];
}

}

您可以在 [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController"object:@"your string"]; 中传递字符串,就像在对象中这样。 也许这会对您有所帮助。

关于ios - 如何在收到本地通知数据后创建传递给使用 objective-c 的主视图 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26965422/

相关文章:

iphone - 从 iPhone 应用程序中启动指南针应用程序

iphone - 无法编译连接iOS5

ios - iOS SVPullToRefresh同时“加载更多”和“刷新”

ios - 为什么 rightRevealToggle 在 'SWRevealViewController' 中什么都不做?

javascript - 如何使用phonegap localnotification插件清除通知栏上的项目

flutter - 有没有办法使用 flutter 本地通知同时安排多个通知

iPhone:如何构建自己的 TabBar?

iphone - 为回合制棋盘游戏编写 AI

ios - UITextField shouldChangeCharactersInRange 在 A 到 B 之间输入值

ionic-framework - Ionic 4 本地通知声音在 Oreo+ 中不起作用