当应用程序第一次启动时调用didReceiveRemoteNotification

我已经实现了我的didReceiveRemoteNotification方法。 它工作并显示一个视图控制器与通过通知数据。 这只适用于应用程序已经在前台或运行在后台。 但是,当应用程序未运行且用户单击通知时,应用程序将启动,但看起来好像没有收到通知。 通知没有写入文本文件,并且viewcontroller没有被推送。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { if ( application.applicationState == UIApplicationStateActive) { NSDictionary *apsInfo = [userInfo objectForKey:@"aps"]; NSString *alertMsg = @""; NSString *badge = @""; NSString *sound = @""; NSString *custom = @""; if( [apsInfo objectForKey:@"alert"] != NULL) { alertMsg = [apsInfo objectForKey:@"alert"]; } if( [apsInfo objectForKey:@"badge"] != NULL) { badge = [apsInfo objectForKey:@"badge"]; } if( [apsInfo objectForKey:@"sound"] != NULL) { sound = [apsInfo objectForKey:@"sound"]; } if( [userInfo objectForKey:@"Type"] != NULL) { custom = [userInfo objectForKey:@"Type"]; } // Set your appending text. NSString *textToAdd = [NSString stringWithFormat:@":%@", alertMsg]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory]; NSString *fileContents = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil]; NSString *textToFile; if (fileContents == NULL) { textToFile = alertMsg; } // Here you append new text to the existing one if (fileContents != NULL) { textToFile = [fileContents stringByAppendingString:textToAdd]; } // Here you save the updated text to that file paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); documentsDirectory = [paths objectAtIndex:0]; fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory]; NSString *content = textToFile; [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil]; NSArray *fileData = [textToFile componentsSeparatedByString:@":"]; NSMutableArray *tableDataFromFile; tableDataFromFile = [[NSMutableArray alloc] init]; int i = 0; for (i = 1; i < [fileData count]; i++) { [tableDataFromFile addObject:fileData[i]]; } NotificationViewController *vc = [[NotificationViewController alloc] initWithNibName:@"NotificationViewController" bundle:nil]; vc.tableData = tableDataFromFile; UIViewController *root = self.mainNavController.topViewController; NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil]; [self.mainNavController setViewControllers:vcs animated:YES]; } // app was already in the foreground else { while (done == FALSE) { } NSDictionary *apsInfo = [userInfo objectForKey:@"aps"]; NSString *alertMsg = @""; NSString *badge = @""; NSString *sound = @""; NSString *custom = @""; if( [apsInfo objectForKey:@"alert"] != NULL) { alertMsg = [apsInfo objectForKey:@"alert"]; } if( [apsInfo objectForKey:@"badge"] != NULL) { badge = [apsInfo objectForKey:@"badge"]; } if( [apsInfo objectForKey:@"sound"] != NULL) { sound = [apsInfo objectForKey:@"sound"]; } if( [userInfo objectForKey:@"Type"] != NULL) { custom = [userInfo objectForKey:@"Type"]; } // Set your appending text. NSString *textToAdd = [NSString stringWithFormat:@":%@", alertMsg]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory]; NSString *fileContents = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil]; NSString *textToFile; if (fileContents == NULL) { textToFile = alertMsg; } // Here you append new text to the existing one if (fileContents != NULL) { textToFile = [fileContents stringByAppendingString:textToAdd]; } // Here you save the updated text to that file paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); documentsDirectory = [paths objectAtIndex:0]; fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory]; NSString *content = textToFile; [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil]; NSArray *fileData = [textToFile componentsSeparatedByString:@":"]; NSMutableArray *tableDataFromFile; tableDataFromFile = [[NSMutableArray alloc] init]; int i = 0; for (i = 1; i < [fileData count]; i++) { [tableDataFromFile addObject:fileData[i]]; } NotificationViewController *vc = [[NotificationViewController alloc] initWithNibName:@"NotificationViewController" bundle:nil]; vc.tableData = tableDataFromFile; UIViewController *root = self.mainNavController.topViewController; NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil]; [self.mainNavController setViewControllers:vcs animated:YES]; } // app was just brought from background to foreground } 

有人能帮我解决这个问题吗? 一旦didFinishLaunchingWithOptions完成,boolean done设置为true。 我只想要notificationviewcontroller打开并显示通知,如果通知被按下,而应用程序根本没有运行。

你应该添加这样的代码:

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; //Accept push notification when app is not open if (remoteNotif) { [self handleRemoteNotification:application userInfo:remoteNotif]; return YES; } return YES; } 

您可以将逻辑从didReceiveRemoteNotification移动到某个常用函数,并从两个地方调用该函数。 这仅在用户通过点击通知来打开应用时才起作用。 如果用户通过点击应用程序图标来打开应用程序,则通知数据将不会到达该应用程序。

SWIFT代码

 let remoteNotif: AnyObject? = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] //Accept push notification when app is not open if ((remoteNotif) != nil) { self.handleRemoteNotification(remoteNotif!) } func handleRemoteNotification(remoteNotif: AnyObject?){ //handle your notification here } 

@Eran谢谢:)