24

मैंने अपनी didReceiveRemoteNotification विधि लागू की है। यह एक सूचना नियंत्रक के साथ एक दृश्य नियंत्रक काम करता है और प्रदर्शित करता है जो पारित होता है। यह केवल तभी काम करता है जब ऐप पहले से ही अग्रभूमि में था या यदि यह पृष्ठभूमि में चल रहा था। हालांकि, जब ऐप नहीं चल रहा है और उपयोगकर्ता अधिसूचना पर क्लिक करता है, तो ऐप लॉन्च होता है, लेकिन ऐसा लगता है कि कोई अधिसूचना प्राप्त नहीं हुई है। अधिसूचना पाठ फ़ाइल में नहीं लिखी गई है और व्यू कंट्रोलर को धक्का नहीं दिया जा रहा है।कॉलिंग 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 


} 

क्या कोई इस समस्या को हल करने में मेरी सहायता कर सकता है? एक बार किया गया बूलियन किया गया है फिनिशलांचिंगविथऑप्शन पूरा होने के बाद। मैं सिर्फ अधिसूचनाव्यू कंट्रोलर को खोलने और अधिसूचना प्रदर्शित करना चाहता हूं अगर ऐप चालू नहीं होने पर अधिसूचना दबाई जाती है।

उत्तर

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

धन्यवाद! यह पूरी तरह से काम किया। हां, तर्क से दूसरे तर्क में तर्क को स्थानांतरित करना बेहतर होगा, क्योंकि यह कोड को बहुत छोटा बना देगा। –

+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 धन्यवाद :)