ios当应用程序没有运行时推送通知点击处理程序

我有以下代码运行来处理推送通知 –

我收到通知 – >点击它 – >然后执行jsCallBack – >将自定义数据作为parameter passing给JS函数。

但只有当应用程序在后台或前台运行时才有效。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { //Handle notification when the user click it while app is running in background or foreground. [[Pushbots sharedInstance] receivedPush:userInfo]; //Get Custom field data NSString* Value1 = [userInfo objectForKey:@"customval1"]; NSString* Value2 = [userInfo objectForKey:@"customval1"]; NSLog(@"%@", Value1); NSLog(@"%@", Value2); NSString * jsCallBack = [NSString stringWithFormat:@"testfromdelegate('%@','%@')", Value1, Value2]; NSLog(@"%@", jsCallBack); [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack]; } 

我在SO和其他一些博客上阅读过文章,当那些应用程序没有运行的时候, didFinishLaunchingWithOptions会帮助我处理通知。 这个链接有信息,但我不知道我的scheme的实施。

请有人可以帮助我如何使用didFinishLaunchingWithOptions在我的情况? 我所需要的是获得自定义数据字段,即使应用程序不运行时,将它们传递给我的JSCallBack。

我应该在我的delegate.m中同时使用didReceiveRemoteNotificationdidFinishLaunchingWithOptions吗? 另外,当我在我的delegate.m中使用didFinishLaunchingWithOptions ,我应该在delegate.h中做些什么改变?

更新1:我明白,当应用程序被终止时,无法访问有效负载。 而且,当应用程序处于前台或后台时,当有推送通知时,它是didReceiveRemoteNotification 。 但是,当我收到通知时,我无法调用我的JScallback函数,而在应用程序处于后台时,我轻敲了它。 我需要一些关于如何处理这个问题的帮助。

您需要在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions执行以下代码- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

 NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; if (userInfo) { [self application:application didReceiveRemoteNotification:userInfo]; } 

首先,我认为你不能访问应用程序未运行时发出的所有通知。 您只能访问您点击的通知来激活应用程序。 我认为这个线程解释得非常好

在后台执行didReceiveRemoteNotification

对于你的情况,我认为你需要以某种方式保持服务器端的计数器,当应用程序处于活动状态,然后交换它,并检索应用程序错过的。

现在代码来处理你的情况,当启动应用程序,你可以在didFinishLaunchingWithOptions做到这一点,我认为你需要离开你的现有函数didReceiveRemoteNotification因为当应用程序在后台运行时收到通知时会被调用(未终止)

 - (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (localNotif) { NSDictionary *userInfo; NSString* Value1 = [userInfo objectForKey:@"customval1"]; NSString* Value2 = [userInfo objectForKey:@"customval1"]; NSLog(@"%@", Value1); NSLog(@"%@", Value2); NSString * jsCallBack = [NSString stringWithFormat:@"testfromdelegate('%@','%@')", Value1, Value2]; NSLog(@"%@", jsCallBack); [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack]; NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey]; [viewController displayItem:itemName]; // custom method NSString * jsCallBack = [NSString stringWithFormat:@"testfromdelegate('%@','%@')", Value1, Value2]; NSLog(@"%@", jsCallBack); [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack]; } return YES; } 

这适用于我,根据我的问题+更新

如果应用程序在前台或后台 –

 //app is in background/foreground - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { [[Pushbots sharedInstance] receivedPush:userInfo]; //Using Pushbots notification platform - a 3rd party push platform NSString* Value1 = [userInfo objectForKey:@"val1"]; NSString* Value2 = [userInfo objectForKey:@"val2"]; NSString * jsCallBack = [NSString stringWithFormat:@"testfromdelegate('%@','%@')", Value1, Value2]; // If the app is in the foreground just execute the javascript if ( application.applicationState == UIApplicationStateActive ) { [self.viewController.webView stringByEvaluatingJavaScriptFromString: jsCallBack]; } else { // If the app is in background, then force to execute the js code on the main thread [self.viewController.webView performSelectorOnMainThread:@selector(stringByEvaluatingJavaScriptFromString:) withObject:jsCallBack waitUntilDone:NO] } } 

如果应用程序处于closures状态,请将其添加到didFinishLaunchingWithOptions

 //Don't remove the existing code, just paste this before the return YES [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cordovaDidLoad) name:CDVPageDidLoadNotification object:self.viewController.webView]; if (launchOptions) { //check if launchOptions is not nil NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; self.myUserInfo = userInfo; } 

并添加这将在Cordova加载后调用 –

 - (void)cordovaDidLoad { //Check that myUserInfo isn't null, that will mean that the app was completely closed and it was opened from the push notification if (self.myUserInfo) { NSString* Value1 = [self.myUserInfo objectForKey:@"val1"]; NSString* Value2 = [self.myUserInfo objectForKey:@"val2"]; NSString * jsCallBack = [NSString stringWithFormat:@"testfromdelegate('%@','%@')", Value1, Value2]; NSLog(@"%@", jsCallBack); [self.viewController.webView stringByEvaluatingJavaScriptFromString: jsCallBack]; } }