Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alberto Huerdo
2,650 PointsiOS Push notification problem when app is closed
Hi, i've setup push notifications for a chat on my app using parse, the notifications are coming and i can handle them when the app is open and when the app is open in the background.
My problem is when i get a new notification and the app is closed, the app execute the same action as if it was open, my problem here is that when the app is closed i want the app to take the user to the chat view but if the app is open only show an alert. My code in my app delegate is this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions != nil)
{
if ([PFUser currentUser]) {
NSDictionary *dict = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
NSString *oID = [dict objectForKey:@"d"][@"objectId"];
[self addMessageFromRemoteNotification:oID updateUI:YES];
}
}
return YES;
}
and
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
...
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
NSLog(@"inactive");
[self addMessageFromRemoteNotification:userInfo[@"d"][@"objectId"] updateUI:YES];
} else {
[PFPush handlePush:userInfo];
}
}