24

私はdidReceiveRemoteNotificationメソッドを実装しました。これは、通過した通知データを含むView Controllerを表示して表示します。これは、アプリケーションがすでにフォアグラウンドにあった場合、またはバックグラウンドで実行されている場合にのみ機能します。ただし、アプリが実行されておらず、ユーザーが通知をクリックするとアプリが起動しますが、通知が受信されていないかのように表示されます。通知はテキストファイルに書き込まれず、ビューコントローラはプッシュされません。アプリが最初に起動したときにdidReceiveRemoteNotificationを呼び出す

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


} 

誰かがこの問題を解決するのに手伝ってください。 boolean doneは、didFinishLaunchingWithOptionsが完了するとtrueに設定されます。アプリケーションがまったく実行されていない間に通知が押された場合、notificationviewcontrollerを開いて通知を表示するだけです。

答えて

33

あなたはあなたのコードにこのような何かを追加する必要があります

- (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からロジックを移動し、両方の場所からその関数を呼び出すことができます。これは、ユーザーが通知をタップしてアプリを開いた場合にのみ機能します。ユーザーがアプリアイコンをタップしてアプリを開くと、通知データはアプリに届きません。

+0

ありがとうございます!これは完全に機能しました。はい、コードをもっと短くするので、ロジックをdidReceiveRemoteNotificationから別のメソッドに移動する方がよいでしょう。 –

+0

あなたは大歓迎です! – Eran

+0

@Eran迅速なコードを入力してください。 – vinbhai4u

0

スウィフトコード

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感謝:)

関連する問題