I want to open a specific view controller when a user clicks on the received push notification message, but when I receive a push notification message and click the message, only the application opens, but it does not redirect to a specific view controller.
My code is
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (applicationIsActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Bildirim"
message:[NSString stringWithFormat:@"%@ ",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
UIViewController *vc = self.window.rootViewController;
PushBildirimlerim *pvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"PushBildirimlerim "];
[vc presentViewController:pvc animated:YES completion:nil];
}
}
My question is related with the iOS push notifications.
There is an extra space in the identifier name. Remove it and try:
You may be having issues with the
if (applicationIsActive)
condition.Put a breakpoint on
-didReceiveRemoteNotification
and see whether it executes in different scenarios and see if it goes within the if-condition.(unrelated to a certain extent but worth checking) this question:
didReceiveRemoteNotification when in background
Note:
-didReceiveRemoteNotification
will not execute if your app was (initially) closed and you clicked on the push notification to open the app.This method executes when a push notification is received while the application is in the foreground or when the app transitions from background to foreground.
Apple Reference: https://developer.apple.com/documentation/uikit/uiapplicationdelegate
So... When the app is not running and a push notification is received, when the user clicks on the push notification, the app is launched and now... the push notification contents will be available in the
-didFinishLaunchingWithOptions:
method in it'slaunchOptions
parameter.In other words...
-didReceiveRemoteNotification
won't execute this time and you'll also need to do this:Also read Apple's Doc on Handling Local and Remote Notifications
I was having same problem that when app is suspended/terminated and push notification arrives my app was only opening and not redirecting to specific screen corresponding to that notification the solution is,
in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
this method the parameterlaunchOptions
tells us if it has the notification by checking that we need to call the method to redirect to specific screenthe code is as below...
then in the method showOfferNotification:notification you can redirect user to corresponding screen like...