Ответ 1
Вы не можете обнаружить этот случай, потому что приложение не открыто, используя push-уведомление (оно было открыто через значок приложения).
Попробуйте открыть приложение, щелкнув push-уведомление.
EDIT:
Если вы хотите быть вызванным для push-уведомления (с помощью фоновой выборки, когда ваше приложение неактивно), вы должны попросить своего стороннего разработчика установить "content-available": 1
в push-уведомлении.
После этого будет вызываться -application:didReceiveRemoteNotification:fetchCompletionHandler:
(при получении push-уведомления), поэтому вы можете сохранить полезную нагрузку в файл, а затем, когда приложение будет открыто, вы можете прочитать файл и предпринять действия.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"#BACKGROUND FETCH CALLED: %@", userInfo);
// When we get a push, just writing it to file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"userInfo.plist"];
[userInfo writeToFile:filePath atomically:YES];
completionHandler(UIBackgroundFetchResultNewData);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Checking if application was launched by tapping icon, or push notification
if (!launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"userInfo.plist"];
[[NSFileManager defaultManager] removeItemAtPath:filePath
error:nil];
NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:filePath];
if (info) {
// Launched by tapping icon
// ... your handling here
}
} else {
NSDictionary *info = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
// Launched with swiping
// ... your handling here
}
return YES;
}
Также не забудьте включить "Удаленные уведомления" в "Фоновых режимах",