簡體   English   中英

應用未運行時,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文章,這些文章可以在應用程序完全不運行時幫助我處理通知。 該鏈接具有信息,但是不確定我的方案的實現。

在我的情況下,請有人能幫助我如何使用didFinishLaunchingWithOptions嗎? 我需要做的就是獲取自定義數據字段,即使應用未運行,也將其傳遞給我的JSCallBack。

我應該在我的委托didFinishLaunchingWithOptions中同時包含didReceiveRemoteNotificationdidFinishLaunchingWithOptions嗎? 另外,當我還在我的委托didFinishLaunchingWithOptions中使用didFinishLaunchingWithOptions ,我應該對委托didFinishLaunchingWithOptions進行哪些更改?

更新1:我了解到,當應用終止時,無法訪問有效負載。 而且,當應用程序處於前台或后台時,當存在推送通知時, didReceiveRemoteNotification才起作用。 但是,當我收到通知並在應用程序處於后台時輕按它時,我無法調用我的JS回調函數。 我需要一些有關如何處理此問題的幫助。

您需要在- (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;
}

這對我有用,按照我的問題和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];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM