gpt4 book ai didi

ios - 首次启动应用程序时调用 didReceiveRemoteNotification

转载 作者:IT王子 更新时间:2023-10-29 07:57:34 24 4
gpt4 key购买 nike

我已经实现了我的 didReceiveRemoteNotification 方法。它工作并显示一个 View Controller ,其中包含传递的通知数据。这仅在应用程序已经在前台或在后台运行时才有效。但是,当应用程序未运行且用户单击通知时,应用程序启动,但看起来好像没有收到通知。通知没有写入文本文件,也没有推送 View Controller 。

- (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 完成后, bool 值 done 将设置为 true。如果在应用程序根本没有运行时按下通知,我只希望 notificationviewcontroller 打开并显示通知。

最佳答案

你应该在你的代码中添加这样的东西:

    - (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 移动到一些通用函数并从两个地方调用该函数。这仅在用户通过点击通知打开应用程序时才有效。如果用户通过点击应用图标打开应用,通知数据将不会到达应用。

关于ios - 首次启动应用程序时调用 didReceiveRemoteNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14968578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com