मैंने अपनी 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
}
क्या कोई इस समस्या को हल करने में मेरी सहायता कर सकता है? एक बार किया गया बूलियन किया गया है फिनिशलांचिंगविथऑप्शन पूरा होने के बाद। मैं सिर्फ अधिसूचनाव्यू कंट्रोलर को खोलने और अधिसूचना प्रदर्शित करना चाहता हूं अगर ऐप चालू नहीं होने पर अधिसूचना दबाई जाती है।
धन्यवाद! यह पूरी तरह से काम किया। हां, तर्क से दूसरे तर्क में तर्क को स्थानांतरित करना बेहतर होगा, क्योंकि यह कोड को बहुत छोटा बना देगा। –
आपका स्वागत है! – Eran
@Eran क्या आप कृपया स्विफ्ट कोड प्रदान कर सकते हैं? – vinbhai4u