简体   繁体   English

如何处理点击推送通知 ios

[英]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)我设法收到推送通知,但我不知道如何处理到达应用程序的推送通知上的点击,我想根据通知类型告诉应用程序何时点击 go 进行特定活动,而不仅仅是打开应用程序(默认行为)

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在这个 function 中,但是当我执行它时,通知不会出现,只是在这个 function 中执行操作

For anyone struggle with this at 2022, Here's a working solution.对于在 2022 年遇到此问题的任何人,这是一个可行的解决方案。 You need to implement the didReceive function inside AppDelegate after connecting your push notification services and than get the notification content:连接推送通知服务后,您需要在 AppDelegate 中实现 didReceive function 并获取通知内容:

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.请注意,如果您需要在推送本身中获取自定义数据,它将存储在 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 像这样覆盖didReceiveRemoteNotification

- (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 如果您想通过点击pushnotification来导航某些特定的ViewController,只需在didReceiveRemoteNotification中添加ViewController导航代码

-(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. 如果应用程序未运行,则应该使用委托方法application:didFinishLaunchingWithOptions:中的launchOptions获取通知有效负载,并根据有效负载进行导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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