【问题标题】:Receiving Newsstand Notifications Only When App Is Running仅在应用程序运行时接收报亭通知
【发布时间】:2013-03-03 13:49:07
【问题描述】:

当应用程序未运行时,我没有收到报亭通知,这就是我所做的。

该应用具有正确的 plist 键“UINewsstandApp = YES”和“UIBackgroundModes = newsstand-content”。

在应用程序委托中,我注册了所有通知类型,并从服务器端(我使用的是名为 Grocer 的 gem)接收来自 APNS 的令牌,我设置了开发证书并发送常规推送,它可以工作。

如果我发送报摊推送,如果应用程序在“didReceiveRemoteNotification”上运行,我会收到它,但是当应用程序未运行时,我在通知中心什么也得不到,这主要是因为“Grocer”具有以下有效负载 {"aps": {"content-available":1}} 并且不能添加任何其他键(警报、徽章等)

所以我认为我不应该在通知中心得到任何东西,我在启动选项中查找'UIApplicationLaunchOptionsRemoteNotificationKey',然后编写一个文件以确保应用程序在后台运行,该文件永远不会写入像这样

NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if(remoteNotif)
    {
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filePath = [cachePath stringByAppendingPathExtension:@"pushreceived.txt"];
        [@"testing" writeToFile:filePath atomically:YES encoding:NSASCIIStringEncoding error:nil];
    }

我在我的用户默认设置中将 NKDontThrottleNewsstandContentNotifications 设置为 true,然后我进行同步以确保。

当应用程序运行时,无论我发送多少次推送,我总是会在“didReceiveRemoteNotification”上收到回调,并带有正确的“内容可用”

如果应用程序已关闭或在后台,则不会发生任何事情。

更新:

我设法更改了发送通知负载的 gem,这是它发送的字典

{"aps"=>
   {
     "alert"=>"Hello iPhone!!",
     "content-available"=>1,
     "badge"=>1,
     "sound"=>"default"
   }
}

这里是我在应用程序上收到的用户信息字典(运行时)

{
    aps =     {
        alert = "Hello iPhone!!";
        badge = 1;
        "content-available" = 1;
        sound = default;
    };
}

请注意 content-available 周围的引号,这是否意味着 APNS 将其解析为自定义键?

【问题讨论】:

    标签: ios objective-c apple-push-notifications newsstand-kit


    【解决方案1】:

    要在您的应用程序在后台运行时接收通知,您需要在主类中添加一个applicationDidEnterBackgroud 方法。来自Apple documentation

    当应用程序在后台运行并且某些消息、数据或其他用户可能感兴趣的项目到达时,应用程序也可能会发现本地通知很有用。在这种情况下,他们应该使用 UIApplication 方法 presentLocalNotificationNow 立即呈现通知:(iOS 为应用程序提供了在后台运行的有限时间)。 Listing 2-2 说明了如何做到这一点。


    - (void)applicationDidEnterBackground:(UIApplication *)application {
        NSLog(@"Application entered background state.");
        // bgTask is instance variable
        NSAssert(self->bgTask == UIInvalidBackgroundTask, nil);
    
        bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [application endBackgroundTask:self->bgTask];
                self->bgTask = UIInvalidBackgroundTask;
            });
        }];
    
        dispatch_async(dispatch_get_main_queue(), ^{
            while ([application backgroundTimeRemaining] > 1.0) {
    
                // Replace this code with your notification handling process
    
                NSString *friend = [self checkForIncomingChat];
                if (friend) {
                    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
                    if (localNotif) {
                        localNotif.alertBody = [NSString stringWithFormat:
                            NSLocalizedString(@"%@ has a message for you.", nil), friend];
                        localNotif.alertAction = NSLocalizedString(@"Read Message", nil);
                        localNotif.soundName = @"alarmsound.caf";
                        localNotif.applicationIconBadgeNumber = 1;
                        [application presentLocalNotificationNow:localNotif];
                        [localNotif release];
                        friend = nil;
                        break;
                    }
                }
            }
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIInvalidBackgroundTask;
        });
    
    }
    

    还有note:

    因为非运行应用程序支持的唯一通知类型是图标标记,只需将 NSRemoteNotificationTypeBadge 作为 registerForRemoteNotificationTypes: 的参数传递。

    【讨论】:

    • 报刊亭通知“理论上”应该能够启动应用程序,即使它被 'UIApplicationLaunchOptionsRemoteNotificationKey' 关闭 .. 这不会发生在 atm
    • @Zoidberg 这个答案看起来有用吗? stackoverflow.com/a/4296282/187954
    • 可悲的是,事实并非如此,这基本上就是我在问题代码中所做的:
    【解决方案2】:

    试试这些作为健全性检查:

    • 在“设置”->“报亭”中,您可以像打开应用一样自动下载内容。
    • 确保您在应用中使用了[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeNewsstandContentAvailability] somehwere。
    • 确保您在 plist 中已将所需的后台模式设置为“newsstand-content”。
    • 在设备中运行应用程序,而不是在模拟器中。

    如果您正确完成了这些操作,即使您的应用未运行,您也应该在didReceiveRemoteNotification 中收到通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多