24

Ich habe meine Methode didReceiveRemoteNotification implementiert. Es funktioniert und zeigt einen View-Controller mit den Benachrichtigungsdaten an, die übergeben werden. Dies funktioniert nur, wenn die App bereits im Vordergrund war oder wenn sie im Hintergrund lief. Wenn die App jedoch nicht ausgeführt wird und der Benutzer auf eine Benachrichtigung klickt, wird die App gestartet, es scheint jedoch so, als ob keine Benachrichtigung empfangen wurde. Die Benachrichtigung wird nicht in die Textdatei geschrieben und der Viewcontroller wird nicht gedrückt.DidReceiveRemoteNotification aufrufen, wenn App zum ersten Mal gestartet wird

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


} 

Könnte mir bitte jemand helfen, dieses Problem zu lösen? Der boolesche Wert done wird auf true gesetzt, sobald didFinishLaunchingWithOptions abgeschlossen ist. Ich möchte nur den Notificationviewcontroller öffnen und die Benachrichtigung anzeigen, wenn eine Benachrichtigung gedrückt wird, während die App überhaupt nicht ausgeführt wird.

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

Sie können die Logik von didReceiveRemoteNotification bis zu einem gewissen gemeinsamen Funktion bewegen und rufen Sie diese Funktion aus beiden Orten:

Antwort

33

Sie sollten so etwas zu Ihrem Code hinzuzufügen. Dies funktioniert nur, wenn der Benutzer die App öffnet, indem er auf die Benachrichtigung klickt. Wenn der Nutzer die App öffnet, indem er auf das App-Symbol klickt, erreichen die Benachrichtigungsdaten die App nicht.

+0

Vielen Dank! Das hat perfekt funktioniert. Ja, es wäre besser, die Logik von didReceiveRemoteNotification auf eine andere Methode zu verschieben, da dies den Code viel kürzer machen würde. –

+0

Gern geschehen! – Eran

+0

@Eran Können Sie bitte den Swift-Code angeben? – vinbhai4u

0

Swift Code

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 Dank :)