【问题标题】:If application is in background state and launch on locanotification tap Which methods called in appdelegate如果应用程序处于后台状态并在 locanotification 点击​​哪些方法在 appdelegate 中调用
【发布时间】:2014-03-14 07:07:41
【问题描述】:

在我的应用程序中,我使用的是 uilocalnotifications。每件事都很好,但只有一件事。我需要显示通知的警报正文。如果应用程序处于前台状态很好,但如果应用程序处于后台状态并发生通知,当我点击该 didReceiveLocalNotification 时不会被调用。显然当时也没有调用 didFinishLaunchingWithOptions 。那么我应该怎么做来处理通知。我正在使用 ios7 和 xcode5。如果您能帮助我,请提前非常感谢。

【问题讨论】:

    标签: ios iphone objective-c uilocalnotification appdelegate


    【解决方案1】:

    对于不在前台的应用,本地通知随后可以在

    -applicationDidFinishLaunchingWithOptions方法

    UILocalNotification *localNotif =
            [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (localNotif) {
            //Handle local notification here.
        }
    

    您可以阅读 Apple 处理通知的文档here

    如果应用当前在内存中,您可以通过以下方式检查其状态:

    - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
    {
        if (app.applicationState == UIApplicationStateInactive )
        {
            NSLog(@"app not running");
        }
        else if(app.applicationState == UIApplicationStateActive )
        {
            NSLog(@"app running");
        }
    }
    

    【讨论】:

    • 感谢 akashg 的即时回复。但是这个方法没有被调用。因为应用已经启动了。
    • 在此处检查以进一步了解:stackoverflow.com/questions/4136333/…
    • 在该页面上获得最多投票的答案应该会有所帮助。
    • 我已经检查过了,但是当我调试 didReceiveLocalNotification 时没有像我提到的那样被调用。
    • @MarkusRautopuro:请参阅答案的第一部分。如果应用程序被杀死并且您点击通知,则会调用“-applicationDidFinishLaunchingWithOptions”。否则“didReceiveLocalNotification”。编辑了答案。
    【解决方案2】:

    如果应用程序已关闭并发出通知,那么您必须在 appdidfinishlaunching 方法中编写以下代码

    // Handle launching from a notification
        UILocalNotification *objLocalNotif =
        [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (objLocalNotif)
        {
            NSLog(@"At the time of launching Recieved Notification %@",objLocalNotif);
            //Do your stuff here
        } 
    

    如果应用程序在后台并且当任何本地通知被引发时,应用程序委托的以下方法被调用。

    - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
        // Handle the notificaton when the app is running
    
        NSLog(@"Recieved Notification %@",notif);
        //do your stuff here
    }
    

    【讨论】:

      【解决方案3】:

      在下面的方法中编写你的代码

      - (void)applicationWillEnterForeground:(UIApplication *)application
      

      当您再次打开尚未完全终止但仍在后台运行的应用程序时将调用它

      【讨论】:

      • 您如何知道应用程序通过点击本地通知进入前台,而不仅仅是通过点击应用程序图标?
      猜你喜欢
      • 2011-09-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多