Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didReceiveRemoteNotification take user to the correct view

I've got a chat application where my server sends out push notifications when a new message is send. The problem I'm having is how can i take the user to the correct view? Im sending a channelID in the push notification but how can i retrieve it and take the user to the actual conversation?

I'm using this code to detect when a push notification was clicked

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
         //opened from a push notification when the app was on background
    }
}
like image 423
Alosyius Avatar asked Jan 28 '26 02:01

Alosyius


1 Answers

If you are sending channelID in push notification than you can retrieve channelID from userInfo dictionary. As midhere said -

1) When application is running in background and When application is running in foreground application:didReceiveRemoteNotification: method will called as below.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive)
     {
     //opened from a push notification when the app was on background

       NSString channelID = [[userInfo objectForKey:@"aps"] objectForKey:@"channelID"];
       NSLog(@"channelID->%@",channelID);
     }
  else if(application.applicationState == UIApplicationStateActive)
     {
     // a push notification when the app is running. So that you can display an alert and push in any view

       NSString channelID = [[userInfo objectForKey:@"aps"] objectForKey:@"channelID"];
       NSLog(@"channelID->%@",channelID);
     }
}

2) When application is not launched (close) than application:didFinishedLaunchWithOptionsmethod will called.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if (launchOptions != nil)
    {
         //opened from a push notification when the app is closed
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo != nil)
        {
            NSString channelID = [[userInfo objectForKey:@"aps"] objectForKey:@"channelID"];
            NSLog(@"channelID->%@",channelID);
        }

    }
     else{
             //opened app without a push notification.
         }
}
like image 94
Rahul Patel Avatar answered Jan 29 '26 18:01

Rahul Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!