简体   繁体   中英

How to handle on click on push notifications ios

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

For anyone struggle with this at 2022, Here's a working solution. You need to implement the didReceive function inside AppDelegate after connecting your push notification services and than get the notification content:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    let title = response.notification.request.content.title
    let message = response.notification.request.content.body
    userDefaultsManager.savePushNotificationData(pushNotificationData: ["title" : title, "message" : message])
}

note that in case you need to get custom data inside the push itself it will be store inside the userInfo. You can get it like that:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                        didReceive response: UNNotificationResponse,
                        withCompletionHandler completionHandler: @escaping () -> Void) {
     let userInfo = response.notification.request.content.userInfo
     let url = userInfo["url"]
}

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];
                        });

                    }
        }

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];

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM