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

时间:2013-12-03 10:50:00

标签: ios web push

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

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

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

在android中我拿第一个字并添加到网页网址: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"];

1 个答案:

答案 0 :(得分:4)

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

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];
}