[英]How to handle on click on push notifications ios
我设法收到推送通知,但我不知道如何处理到达应用程序的推送通知上的点击,我想根据通知类型告诉应用程序何时点击 go 进行特定活动,而不仅仅是打开应用程序(默认行为)
我知道我可以收到
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"Here I should Recive The notification .... ") ;
//call the commented functions .....
}
在这个 function 中,但是当我执行它时,通知不会出现,只是在这个 function 中执行操作
对于在 2022 年遇到此问题的任何人,这是一个可行的解决方案。 连接推送通知服务后,您需要在 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])
}
请注意,如果您需要在推送本身中获取自定义数据,它将存储在 userInfo 中。 你可以这样得到它:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
let url = userInfo["url"]
}
尝试处理此点击,如代码所示,
- (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.
像这样覆盖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];
});
}
}
如果您想通过点击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];
}
仅当应用程序处于活动状态时,该委托方法才会被触发。 如果应用程序未运行,则应该使用委托方法application:didFinishLaunchingWithOptions:
中的launchOptions
获取通知有效负载,并根据有效负载进行导航。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.