我已经实现了didReceiveRemoteNotification方法。它工作并显示一个视图控制器,其中包含通过的通知数据。这仅适用于应用程序已在前台或后台运行的情况。但是,当应用程序未运行且用户单击通知时,应用程序将启动,但看起来好像没有收到任何通知。通知未写入文本文件,并且未按下viewcontroller。
- (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后,boolean done设置为true。我只是想让notificationviewcontroller打开并显示通知,如果在应用程序根本没有运行时按下通知。
答案 0 :(得分: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
移动到某个常用功能,并从两个位置调用该功能。这仅在用户通过点击通知打开应用时才有效。如果用户通过点按应用图标打开应用,则通知数据将无法到达应用。
答案 1 :(得分:0)
Swift Code
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谢谢:)