gpt4 book ai didi

ios - 将参数从推送通知传递到 viewDidLoad

转载 作者:行者123 更新时间:2023-11-28 19:06:09 26 4
gpt4 key购买 nike

我有一个 ios 应用程序,它加载网页以显示数据。我使用推送通知来接收新闻,我希望根据收到的推送,转到一个页面或其他页面(同一页面内的部分)。

在推送通知收到的文本中,我在文本前加了一个词,比如:

page1 - text
page2 - text2
page3 - text3
...

在 android 中,我取第一个词并添加到网页 url:www.page.com/+ pageAdded

在 iOs 中,我认为我必须将这段代码添加到 didFinishLaunchWithOptions 函数中。但是我不知道应该在哪里添加用于传递参数的新代码。我添加了我的函数,这样你就可以告诉我把它放在哪里。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"CityInfoPush" accessGroup:nil];
udid = [keychainItem objectForKey:(__bridge id)kSecValueData];

//if udid is empty , that means we need to generate one and save it on KeyChain
if([udid length] == 0){
NSLog(@"No CFUUID found. Creating one...");
//creating CFUUID
CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
NSString *cfuuidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
NSLog(@"Device CFUUID created is : %@" , cfuuidString);
//saving CFUUID on KeyChain
[keychainItem setObject: cfuuidString forKey:(__bridge id)kSecValueData];

//retrieving CFUUID from KeyChain and passing it to udid String.
udid = [keychainItem objectForKey:(__bridge id)kSecValueData];
}

//For reseting the keyChain (testing)
//[keychainItem resetKeychainItem];

NSLog(@"Password Saved in KeyChain is: %@" , udid);


self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

return YES;
}

我将消息添加到有效负载中,以便像这样将其发送到苹果服务器:

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);

// Encode the payload as JSON
$payload = json_encode($body);

我正尝试像这样在 ios 中获取警报文本(消息),但总是失败并使应用程序崩溃:

NSString *text=[[lastNotif valueForKeyPath:@"aps"][0] objectForKey:@"alert"];

最佳答案

首先,您可以检查您的应用程序是否已在您的 应用程序中通过 puch 通知启动:
didFinishLaunchingWithOptions:
功能:

if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey])

我所做的是将此通知保存在 userDefaults 中:

[[NSUserDefaults standardUserDefaults] setObject:[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey] forKey:@"notificationReceived"];
[[NSUserDefaults standardUserDefaults] synchronize];

然后在您想要捕获推送通知的 viewDidLoad 中,检查内存中是否有推送通知:

NSDictionary *lastNotif = [[NSUserDefaults standardUserDefaults] objectForKey:@"notificationReceived"];
if (lastNotif != nil)
{
//handle what you want to do with your notification

//now don't forget to clean userDefaults
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"notificationReceived"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

关于ios - 将参数从推送通知传递到 viewDidLoad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20349061/

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