我已经实现了我的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
}
可能有人请帮我解决这个问题呢? 一旦didFinishLaunchingWithOptions完成完成的布尔值被设置为true。 我只想notificationviewcontroller打开,如果同时应用程序不运行在所有被按下的通知显示通知。