Apple doc在后台引起用户注意说
通知是应用程序暂停、在后台或未运行以引起用户注意的一种方式。
由于区域监控,我的应用程序被 iOS 唤醒is in the background
并发布本地通知。用户点击通知,应用程序将在前台。
由于用户点击了通知,如何确定应用程序进入前台?
哪个委托方法将包含通知信息。
didFinishLaunchingWithOption or didReceiveLocalNotification
Apple doc在后台引起用户注意说
通知是应用程序暂停、在后台或未运行以引起用户注意的一种方式。
由于区域监控,我的应用程序被 iOS 唤醒is in the background
并发布本地通知。用户点击通知,应用程序将在前台。
由于用户点击了通知,如何确定应用程序进入前台?
哪个委托方法将包含通知信息。
didFinishLaunchingWithOption or didReceiveLocalNotification
如果您的应用程序在后台运行并且您在 LocalNotification Banner 上被点击,那么您将被调用以下方法:
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
iOS 8 之后:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
如果应用程序未在后台运行,您将在以下位置收到通知:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if ([launchOptions valueForKey:@"UIApplicationLaunchOptionsLocalNotificationKey"]) {
// here you will get
}
当 UILocalNotification 被触发时,您可以检测到您的应用程序的状态是什么,如果 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 被调用,这将确保收到本地通知。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
// Application was in the background when notification was delivered.
} else {
}
}
您可以通过使用 application.applicationState 来确定这一点
if(application.applicationState == UIApplicationStateInactive)
{
NSLog(@" Inactive");
// when you tapping on notification
}
else if (application.applicationState == UIApplicationStateBackground)
{
NSLog(@" background");
}
else
{
NSLog(@" Active");
}