How to handle on click on push notifications ios

2019-09-02 13:30发布

问题:

I managed to recive a push notification, but i don't know how to handle the click on the push notification that arrive to the application, i want to tell the application when click go to specific activity based on the type of notification, not just open the app (Default behavior)

i know that i can receive

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

    NSLog(@"Here I should Recive The notification .... ") ;
    //call the commented functions .....
}

in this function , but when i implement it, the notification don't appear and just do what in this function

回答1:

Try to handle this the click as shown in code,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

//Accept push notification when the app is not open.
        if (remoteNotifiInfo) {
            [self application:application didReceiveRemoteNotification: remoteNotifiInfo];
        }

}



   // On click of notification open related screen.

override didReceiveRemoteNotification like this

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
        {
                    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
                    if ( state == UIApplicationStateInactive )
                    {
                        //Your actions on notification click
                        double delayInSeconds = 0.3;
                        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                            //code to be executed on the main queue after delay
                            [[NSNotificationCenter defaultCenter] postNotificationName:@"pushTOEventDetails" object:eventVO];
                        });

                    }
        }


回答2:

If you want to navigate some specific ViewController by tapping pushnotification Just add ViewController navigation code in didReceiveRemoteNotification

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

NSLog(@"Here I should Receive The notification .... ") ;
//call the commented functions .....

FirstController *firstviewcontroller = [[FirstController alloc] initWithNibName:@"FirstController" bundle:nil];

    [self.navigationController pushviewcontroller:firstviewcontroller animated:YES];

}


回答3:

That delegate method will only get fired if the application is active. If the application is not running, you should get the notification payload using launchOptions in the delegate method application:didFinishLaunchingWithOptions: and do navigation according to the payload.