当应用程序未运行时,ios推送通知单击处理程序

时间:2015-06-08 05:12:12

标签: ios cordova push-notification

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

我收到通知 - >点击它 - >然后执行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中应该同时拥有didReceiveRemoteNotificationdidFinishLaunchingWithOptions吗?另外,当我在delegate.m中也使用didFinishLaunchingWithOptions时,我应该在delegate.h中进行哪些更改?

更新1:我了解到,当应用程序终止时,无法访问有效负载。并且,当app处于前台或后台时,didReceiveRemoteNotification在有推送通知时有效。 但是,当我收到通知时,我无法调用我的JS回调函数,而在应用程序处于后台时我点击了它。 我需要一些帮助来解决这个问题。

3 个答案:

答案 0 :(得分:1)

您需要在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

中执行以下代码
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
    [self application:application didReceiveRemoteNotification:userInfo];
}

答案 1 :(得分:0)

首先,我认为您无法访问应用未运行时发出的所有通知。您只能访问您单击以激活应用程序的通知。我认为这个帖子在这里解释得非常好

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

答案 2 :(得分:0)

这对我有用,根据 我的问题+更新 -

的要求

如果应用程序位于前台或后台 -

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