iphone - 应用程序未运行时处理推送通知

标签 iphone push-notification apple-push-notifications

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

当应用程序运行时(无论是作为事件应用程序还是在后台运行),推送通知都可以完美工作,但没有任何效果当应用运行时正确。

我尝试在 application: didFinishLaunchingWithOptions: 中注销 launchOptions NSDictionary 以查看其带来的负载 - 但它显示为“(null)”。所以它基本上不包含任何内容 - 这没有意义,因为它不应该包含通知的负载吗?

有人知道如何在应用程序未运行时推送通知到达时发挥作用吗?

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

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

所以这应该能够处理应用程序的所有状态 - 但事实并非如此。推送通知仅在应用程序运行时起作用 - 无论是在后台还是在前台...

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

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

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 出现 EMPTY...将继续调整...

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

编辑 #3 - 现在几乎 100% 工作
因此,在 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 上选择的内容并进行相应操作。

这与 didReceiveRemoteNotification 逻辑完美配合。

但是...当应用程序未运行时,我向其发送推送通知,如果我没有在推送通知警报到达后立即单击它,而是等待它淡出(这发生在大约 3-4 秒),然后然后我点击应用程序的图标 - 现在其上的 BADGE 为 1 - 应用程序启动,但我根本没有收到推送通知警报它启动了。它就坐在那里。

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

最佳答案

当您的应用程序未运行或终止且您点击推送通知时,此功能将触发;

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

所以你应该这样处理,

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

关于iphone - 应用程序未运行时处理推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12116282/

相关文章:

iphone - 访问 UITableView 中单元格的文本

iphone - 避免同时移动两个 View

ios - 捕获对 "App would like to send you push notifications"警报的回答

c# - Amazon Simple Notification Service "push"可以通知到桌面客户端吗?

ios - 应用程序:didRegisterForRemoteNotificationsWithDeviceToken: not called ios 10. 3.2

ios - 如何在 objective-c 中从APNS反馈服务器接收数据

iphone - 有适用于 iPhone 的游戏引擎吗?

iphone - 将 subview 添加到 UITableViewCell 内容 View

android - Phonegap 推送通知以打开特定的应用程序页面

ios - APNS 确认