应用未运行时,ios推送通知单击处理程序 [英] ios Push notification click handler when the app is not running

查看:223
本文介绍了应用未运行时,ios推送通知单击处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我收到通知->点击它->然后执行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可以在应用程序完全不运行时帮助我处理通知. 此链接包含信息,但不确定其实现我的情况.

在我的情况下,请有人可以帮助我如何使用didFinishLaunchingWithOptions吗?我所需要做的就是获取自定义数据字段,即使应用未运行,也将其传递给我的JSCallBack.

我的委托人.m中应同时包含didReceiveRemoteNotificationdidFinishLaunchingWithOptions吗?此外,当我还在我的委托人.m中使用didFinishLaunchingWithOptions时,我应该对委托人.h进行哪些更改?

更新1:我了解到,当该应用终止时,无法访问有效负载.而且,当应用程序处于前台或后台时,didReceiveRemoteNotification会在出现推送通知时起作用. 但是,当我收到通知并在应用程序处于后台时轻按它时,我无法调用我的JS回调函数. 我需要一些有关如何处理此问题的帮助.

解决方案

这对我有用,如 我的问题+ UPDATE -

中的要求

如果应用程序是在前台还是在后台-

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

I have the following code running to handle a push notification -

I receive the notification -> tap on it -> and then performing a jsCallBack -> passing the custom data as parameters to a JS function.

but this works only when the app is running in background or foreground.

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

I read posts in SO and few other blogs that didFinishLaunchingWithOptions is something that would help me handle the notification when the app is not running at all. This link has info but am not sure about the implementation for my scenario.

Please could someone help me on how to use didFinishLaunchingWithOptions in my case? What all I need is to get the custom data fields, pass them to my JSCallBack even when the app is not running.

Should I have both didReceiveRemoteNotification and didFinishLaunchingWithOptions in my delegate.m? Also, when I am also using didFinishLaunchingWithOptions in my delegate.m, what changes should I make in delegate.h?

UPDATE 1: I understood that, when the app is terminated, one cannot get access to the payload. And, when app is in foreground or background, it is the didReceiveRemoteNotification that works when there is a push notification. But, I am not able to invoke my JS callback function when I received the notification and I tapped on it while the app is in background. I need some help on how to handle this.

解决方案

This works for me, as required in my question + the UPDATE-

If the app is in foreground or in background -

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

If the app is in closed state, add this in 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;
    }

and add this, which will be called after Cordova is loaded -

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

这篇关于应用未运行时,ios推送通知单击处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆