Reputation: 23
I have read many posts on this matter, but none of them really helps me out. It seems quite easy to do, but somehow I can't figure it out.
What am I trying to do here? Well, I send a push notifications to my app. Everything works fine. I can handle it when it is in the background, inactive or even when the app is not running. But one other state - app in foreground - gives me a headache.
I have this code snippet which I'm using. All fine. When the app is running in the foreground, the user gets the UIAlertview. But how do I add an action to the view button? This particular action I mean: when someone taps the view button he/she gets redirected to the url which has been passed through the push notification.
Just like I have used before in the code snippet (this part to be precise):
NSString *notifUrl = [apsInfo objectForKey:@"url"];
NSLog(@"Received Push Url: %@", notifUrl);
I hope this makes sense and someone can help me out. I'm lost at this one ... This is the entire code snippet which is relevant at this subject.
-(void)Redirect:(NSString*)url{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(@"%@",url);
}
/**
* Remote Notification Received while application was open.
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
#if !TARGET_IPHONE_SIMULATOR
NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);
NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
NSString *notifUrl = [apsInfo objectForKey:@"url"];
NSLog(@"Received Push Url: %@", notifUrl);
// app was already in the foreground
if ( application.applicationState == UIApplicationStateActive ) {
UIAlertView *alertPush = [[UIAlertView alloc]initWithTitle: @"Message"
message: alert
delegate: self
cancelButtonTitle:@"View"
otherButtonTitles: @"Cancel", nil];
[alertPush show];
[alertPush release];
}
// app is inactive or in the background
else {
[self Redirect:notifUrl];
}
#endif
}
// what to do if user taps the viewbutton in push notification alert view
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {
NSLog(@"View Button has been tapped");
// We need the url that has been passed by the push notification
}
else {
// Do something
}
}
Upvotes: 2
Views: 868
Reputation: 4279
Just define your variable notifUrl
in .h file so that you can access this in other methods of your class as well. Then store url in that variable and use that in alertview delegate method.
//Add this in .h file
NSString *notifUrl;
//Add this in .m file
-(void)Redirect:(NSString*)url{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(@"%@",url);
}
/**
* Remote Notification Received while application was open.
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
#if !TARGET_IPHONE_SIMULATOR
NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);
NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
//Define this variable in .h file so that you can access this in other methods as well
notifUrl = [apsInfo objectForKey:@"url"];
NSLog(@"Received Push Url: %@", notifUrl);
[notifUrl retain]; // this is retain value of notifUrl so that you can use it later
// app was already in the foreground
if ( application.applicationState == UIApplicationStateActive ) {
UIAlertView *alertPush = [[UIAlertView alloc]initWithTitle: @"Message"
message: alert
delegate: self
cancelButtonTitle:@"View"
otherButtonTitles: @"Cancel", nil];
[alertPush show];
[alertPush release];
}
// app is inactive or in the background
else {
[self Redirect:notifUrl];
}
#endif
}
// what to do if user taps the viewbutton in push notification alert view
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {
NSLog(@"View Button has been tapped");
NSLog(@"Received Push Url: %@", notifUrl);
[self Redirect:notifUrl];
}
else {
// Do something
}
}
Upvotes: 3