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

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

问题描述

当我的应用运行并收到推送通知时,如果我点击该通知,应用会启动 - 但它不会提示用户使用我设置的 Alert-View,询问他们是否要查看通知的内容.它只是启动,然后坐在那里.

当应用运行时,推送通知确实完美运行 - 无论是作为活动应用还是在后台运行 - 但没有任何效果当应用运行时正确.

我尝试注销 application: didFinishLaunchingWithOptions: 中的 launchOptions NSDictionary 以查看它带来的负载 - 但它显示为(空)".所以它基本上什么都不包含 - 这没有意义,因为它不应该包含通知的负载吗?

有人知道如何在应用未运行的情况下在推送通知到达时使其工作吗?

这是我在 application: didReceiveRemoteNotification 中使用的代码,只是为了看看是什么:

if (UIApplicationStateBackground) {NSLog(@"==========================);NSLog(@"应用程序在后台...");}否则如果(UIApplicationStateActive == TRUE){NSLog(@"==========================);NSLog(@"应用程序处于活动状态");}别的 {[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 99];UIAlertView *BOOM = [[UIAlertView alloc] initWithTitle:@"BOOM"消息:@"应用程序处于非活动状态"委托:自己cancelButtonTitle:@"啊哈!"otherButtonTitles:nil];【BOOM秀】;NSLog(@"应用程序未激活");}

所以这应该处理所有应用程序的状态 - 但它不是.推送通知仅在应用运行时有效 - 无论是在后台还是在前台...

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

更新/编辑#2:根据@dianz"建议(如下),我修改了我的 应用程序的代码:didFinishLaunchingWithOptions 以包含以下内容:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];如果(本地通知){NSString *json = [localNotif valueForKey:@"data"];UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"appDidFinishWithOptions"消息:json委托:自己cancelButtonTitle:@"cool"otherButtonTitles:nil];【巴姆秀】;}

这确实使 AlertView 框出现,但似乎没有有效负载:AlertView 的标题显示(appDidFinishWithOptions"),但 json NSString 出现 EMPTY... 将继续调整...

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

编辑 #3 - 它现在几乎 100%
所以,在 didFinishLaunchingWithOptions 中:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];如果(本地通知){//获取pushKey:pushKey = [localNotif valueForKey:@"pushKey"];//"pushKey" 是一个全局声明的 NSString//valueForKey"语句中的pushKey"是我的自定义 JSON 推送通知有效负载的一部分.//pushKey 的值将始终是一个字符串,它将是//我希望应用程序导航到的屏幕.所以当通知到达时,我去//通过 IF 语句并说if pushKey='specials' then push the//specialsViewController on 等//在这里,我解析 JSON 推送通知的警报"部分:NSDictionary *tempDict = [localNotif valueForKey:@"aps"];NSString *pushMessage = [tempDict valueForKey:@"alert"];//最后,询问用户是否想看到推送通知或取消它:UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"(来自 appDidFinishWithOptions)"消息:推送消息委托:自己取消按钮标题:@"取消"otherButtonTitles:@"查看信息", nil];【巴姆秀】;}

我接下来实现了 alertView: clickedButtonAtIndex 方法以查看用户在 AlertView 上选择的内容并相应地进行.

这与 didReceiveRemoteNotification 逻辑完美结合.

然而......当应用程序没有运行时,我向它发送推送通知,如果我没有在推送通知到达后立即点击它而是等待它淡出(这发生在像 3-4 秒),然后然后我点击应用程序的图标 - 现在上面有一个 1 的徽章 - 应用程序启动,但我根本没有收到推送通知警报它启动.它只是坐在那里.

我想我接下来需要计算那个排列...

解决方案

当你的应用没有运行或被终止时,你点击推送通知,这个功能就会触发;

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

所以你应该这样处理,

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];如果(本地通知){NSString *json = [localNotif valueForKey:@"data"];//将字符串解析为字典}

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.

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?

EDIT: 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/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];

}

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...

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

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

    }

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

This, along with the didReceiveRemoteNotification logic works perfectly.

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天全站免登陆