我已經實現了我的didReceiveRemoteNotification方法。它的工作原理是顯示一個視圖控制器和通過的通知數據。這隻適用於應用程序已經在前臺或它在後臺運行。但是,當應用程序未運行且用戶單擊通知時,應用程序將啓動,但看起來好像沒有收到通知。通知沒有寫入文本文件,並且viewcontroller沒有被推送。當應用程序第一次啓動時調用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完成,布爾值done就設置爲true。我只想讓notificationviewcontroller打開並顯示通知,如果在應用程序根本沒有運行時按下通知。
謝謝!這工作完美。是的,將邏輯從didReceiveRemoteNotification移動到另一個方法會更好,因爲它會使代碼更短。 – 2013-02-20 14:37:12
不客氣! – Eran 2013-02-21 02:14:22
@Eran你能提供swift代碼嗎? – vinbhai4u 2015-07-07 14:54:53