tutorial react notification apple ios objective-c xcode push-notification apple-push-notifications

react - push notifications ios swift 4



Llamar a didReceiveRemoteNotification cuando la aplicaciĆ³n se inicia por primera vez (2)

Debe agregar algo como esto a su código:

- (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; }

Puede mover la lógica de didReceiveRemoteNotification a alguna función común y llamar a esa función desde ambos lugares. Esto solo funcionará si el usuario abre la aplicación tocando la notificación. Si el usuario abre la aplicación tocando el ícono de la aplicación, los datos de notificación no llegarán a la aplicación.

Implementé mi método didReceiveRemoteNotification. Funciona y muestra un controlador de vista con los datos de notificación que se pasan. Esto solo funciona cuando la aplicación ya estaba en primer plano o si se estaba ejecutando en segundo plano. Sin embargo, cuando la aplicación no se está ejecutando y el usuario hace clic en una notificación, la aplicación se inicia, pero parece que no se recibió ninguna notificación. La notificación no está escrita en el archivo de texto y no se está presionando el controlador de vista.

- (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 }

¿Podría alguien ayudarme a resolver este problema? El booleano done se establece en verdadero una vez que se completa didFinishLaunchingWithOptions. Solo quiero que el controlador de vista de notificaciones se abra y muestre la notificación si se presiona una notificación mientras la aplicación no se está ejecutando.


código 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 gracias :)