ios 应用未运行时推送通知点击处理程序 运行
ios Push notification click handler when the app is not running
我有以下代码 运行 处理推送通知 -
我收到通知 -> 点击它 -> 然后执行 jsCallBack -> 将自定义数据作为参数传递给 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
可以帮助我在应用程序根本不 运行 时处理通知。
This link has info 但我不确定我的方案的实施情况。
有人可以帮助我了解如何在我的情况下使用 didFinishLaunchingWithOptions
吗?我只需要获取自定义数据字段,即使应用程序不是 运行.
也将它们传递给我的 JSCallBack
我的 delegate.m 中是否应该同时包含 didReceiveRemoteNotification
和 didFinishLaunchingWithOptions
?此外,当我在 delegate.m 中也使用 didFinishLaunchingWithOptions
时,我应该在 delegate.h 中进行哪些更改?
更新 1: 我了解到,当应用程序终止时,无法访问有效负载。而且,当应用程序处于前台或后台时,didReceiveRemoteNotification
在有推送通知时起作用。
但是,当我收到通知并在应用程序处于后台时点击它时,我无法调用我的 JS 回调函数。
我需要一些帮助来解决这个问题。
首先,我认为您无法访问应用未 运行 时出现的所有通知。您只能访问您单击以激活该应用程序的通知。我觉得这个帖子解释得很好
didReceiveRemoteNotification when in background
对于您的场景,我认为您需要以某种方式将计数器保留在服务器端,当应用程序处于活动状态时,然后交换它并检索应用程序遗漏的计数器。
现在,为了让代码在应用程序启动时处理您的情况,您可以在 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;
}
您需要在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中执行以下代码
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self application:application didReceiveRemoteNotification:userInfo];
}
这对我有用,正如我的问题+更新-
中所要求的
如果应用程序在前台或后台 -
//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]
}
}
如果应用处于关闭状态,
在 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];
}
}
我有以下代码 运行 处理推送通知 -
我收到通知 -> 点击它 -> 然后执行 jsCallBack -> 将自定义数据作为参数传递给 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
可以帮助我在应用程序根本不 运行 时处理通知。
This link has info 但我不确定我的方案的实施情况。
有人可以帮助我了解如何在我的情况下使用 didFinishLaunchingWithOptions
吗?我只需要获取自定义数据字段,即使应用程序不是 运行.
我的 delegate.m 中是否应该同时包含 didReceiveRemoteNotification
和 didFinishLaunchingWithOptions
?此外,当我在 delegate.m 中也使用 didFinishLaunchingWithOptions
时,我应该在 delegate.h 中进行哪些更改?
更新 1: 我了解到,当应用程序终止时,无法访问有效负载。而且,当应用程序处于前台或后台时,didReceiveRemoteNotification
在有推送通知时起作用。
但是,当我收到通知并在应用程序处于后台时点击它时,我无法调用我的 JS 回调函数。
我需要一些帮助来解决这个问题。
首先,我认为您无法访问应用未 运行 时出现的所有通知。您只能访问您单击以激活该应用程序的通知。我觉得这个帖子解释得很好
didReceiveRemoteNotification when in background
对于您的场景,我认为您需要以某种方式将计数器保留在服务器端,当应用程序处于活动状态时,然后交换它并检索应用程序遗漏的计数器。
现在,为了让代码在应用程序启动时处理您的情况,您可以在 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;
}
您需要在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self application:application didReceiveRemoteNotification:userInfo];
}
这对我有用,正如我的问题+更新-
中所要求的如果应用程序在前台或后台 -
//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]
}
}
如果应用处于关闭状态,
在 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];
}
}