Reputation: 3823
I have an ios app which loads a webpage to show data. I use push notifications for receiving news and I want that depending of which push is received, goes to one page or other (sections inside the same page).
In the text received in push notification, I add a word before the text, something like:
page1 - text
page2 - text2
page3 - text3
...
In android I take the first word and add to the webpage url: www.page.com/ + pageAdded
In iOs I think I haveto add this code to didFinishLaunchWithOptions function. But I don't know where I should add the new code for passing the arguments. I add my function so you can tell me where to put it.
- (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;
}
I add the message to the payload for sending it to the apple server like this:
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
I am trying to get the alert text (message) in ios like this, but always fails and crash the app:
NSString *text=[[lastNotif valueForKeyPath:@"aps"][0] objectForKey:@"alert"];
Upvotes: 0
Views: 1710
Reputation: 2818
First, you can check if you app has been launched with a puch notification in your application:
didFinishLaunchingWithOptions:
function with :
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey])
What I do is save this notification in the userDefaults :
[[NSUserDefaults standardUserDefaults] setObject:[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey] forKey:@"notificationReceived"];
[[NSUserDefaults standardUserDefaults] synchronize];
Then in the viewDidLoad where you want to catch your push notification, check if you have one in memory :
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];
}
Upvotes: 4