处理推送通知时,应用程序无法运行 [英] Handling Push Notifications when App is NOT running

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

问题描述

在我的应用程序的运行,并收到一个推送通知,如果我点击该通知,应用启动 - 但它不会提示用户随着警报的视图我设置了,询问他们是否要查看通知的内容或没有。它只是启动,并坐在那里。

When my App is not running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn't prompt the user with the Alert-View I set up, asking them whether they want to view the Notification's contents or not. It just launches, and sits there.

的推送通知做的工作的非常的时,应用程序的运行 - 无论是作为活动的应用程序或在后台 - 但没有作品正确时,应用程序的运行。

The Push Notifications do work perfectly when the App is running - either as the Active app or while in the background - but nothing works correctly when the app is not running.

我尝试登录时的 launchOptions 的NSDictionary在的application:didFinishLaunchingWithOptions:来看看有什么加载其自备 - 但说到为(空)。因此,它基本上包含任何 - 这是没有意义的原因不应该是包含通知的负载

I tried logging-out the launchOptions NSDictionary in application: didFinishLaunchingWithOptions: to see what load its bringing - but it comes up as "(null)". So It basically contains nothing - which doesn't make sense cause shouldn't it contain the Notification's load?

任何人有任何想法如何使当他们到达时,而应用程序没有运行推送通知工作?

Anybody have any ideas how to make Push Notifications work when they arrive while the App was NOT running?

编辑:这里的code,我使用应用程序:didReceiveRemoteNotification 只是为了看看什么是什么:

here's the code I'm using in application: didReceiveRemoteNotification just to see what's what:

if (UIApplicationStateBackground) {

    NSLog(@"===========================");
    NSLog(@"App was in BACKGROUND...");
}
else if (UIApplicationStateActive == TRUE) {
    NSLog(@"===========================");
    NSLog(@"App was ACTIVE");
}
else {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 99]; 
    UIAlertView *BOOM = [[UIAlertView alloc] initWithTitle:@"BOOM"
                                                   message:@"app was INACTIVE"
                                                  delegate:self
                                         cancelButtonTitle:@"a-ha!"
                                         otherButtonTitles:nil];
    [BOOM show];
    NSLog(@"App was NOT ACTIVE");
}

因此​​,这是应该采取应用程序的所有状态的护理 - 但它不是。推送通知只工作应用程序运行时 - 无论是在后台或前台...

So this is supposed to take care of all the application's states - but its not. Push Notifications are only working when the app is running - either in the background or in the foreground...

=============================================== =

================================================

UPDATE /编辑#2:
按照@dianz的建议(以下)我修改了应用程序的code中:didFinishLaunchingWithOptions 来包括以下内容:

UPDATE/EDIT#2: as per "@dianz" suggestion (below,) I modified the code of my application: didFinishLaunchingWithOptions to include the following:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
    NSString *json = [localNotif valueForKey:@"data"]; 

    UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"appDidFinishWithOptions"
                                                  message:json   
                                                 delegate:self
                                        cancelButtonTitle:@"cool"
                                        otherButtonTitles:nil];
    [bam show];

}

这确实让AlertView框出现,但似乎没有有效载荷:该AlertView的标题显示出来(appDidFinishWithOptions),但JSON的NSString出现空...将继续调整...

This does make the AlertView box appear, but there seems to be no payload: the title of the AlertView shows up ("appDidFinishWithOptions"), but the json NSString comes up EMPTY... Will keep tweaking...

======================

======================

编辑#3 - 其现在的工作的几乎的100%

所以,在 didFinishLaunchingWithOptions

EDIT #3 - its now working almost 100%
So, in didFinishLaunchingWithOptions:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (localNotif) {
        // Grab the pushKey:
        pushKey = [localNotif valueForKey:@"pushKey"];
        // "pushKey" is an NSString declared globally
        // The "pushKey" in the "valueForKey" statement is part of my custom JSON Push Notification pay-load.
        // The value of pushKey will always be a String, which will be the name of the 
        // screen I want the App to navigate to. So when a Notification arrives, I go
        // through an IF statement and say "if pushKey='specials' then push the                                      
        // specialsViewController on, etc.

        // Here, I parse the "alert" portion of my JSON Push-Notification:
        NSDictionary *tempDict = [localNotif valueForKey:@"aps"];
        NSString *pushMessage = [tempDict valueForKey:@"alert"];


        // Finally, ask user if they wanna see the Push Notification or Cancel it:
        UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"(from appDidFinishWithOptions)"
                                                      message:pushMessage  
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:@"View Info", nil];
        [bam show];

    }

我旁边落实 alertView:clickedButtonAtIndex 方法来查看用户选择的AlertView什么,并进行相应处理。

I next implement the alertView: clickedButtonAtIndex method to see what the user chose on the AlertView and proceed accordingly.

这与 didReceiveRemoteNotification 逻辑一起完美的作品。

This, along with the didReceiveRemoteNotification logic works perfectly.

但...当应用程序没有运行,我送它一个推送通知,如果我不上的推送通知警报,一旦它到达点击,而是等待它淡出(之后会发生像3-4秒),而然后的我点击应用程序图标 - 现在有1就可以了徽章 - 应用程序启动,但我不明白的推送通知警报在所有的时候它启动。它只是坐在那里。

HOWEVER... when the app is NOT running, and I send it a Push Notification, if I DON'T click on the Push Notification alert as soon as it arrives and instead wait for it to fade out (which happens after like 3-4 seconds), and then I click on the App's icon - which now has a BADGE of 1 on it - the app launches, but I don't get the Push Notification alert at all when it launches. It just sits there.

我想我需要弄清楚的的排列在下...

Guess I need to figure that permutation next...

推荐答案

当你的应用程序没有运行,或者死亡,你点击的推送通知这个功能将触发;

When your app is not running or killed and you tap on push notification this function will trigger;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

所以你要处理它这个样子,

so you should handle it like this,

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
    NSString *json = [localNotif valueForKey:@"data"];
    // Parse your string to dictionary
}

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

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