ios - 仅在应用程序运行时接收报亭通知

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

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

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

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

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

所以我认为我不应该在通知中心得到任何东西,我在启动选项中寻找'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”上收到回调,并带有适当的“content-available”

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

更新:

我设法更改了发送通知负载的 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 将其解析为自定义 key ?

最佳答案

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

Applications might also find local notifications useful when they run in the background and some message, data, or other item arrives that might be of interest to the user. In this case, they should present the notification immediately using the UIApplication method presentLocalNotificationNow: (iOS gives an application a limited time to run in the background). Listing 2-2 illustrates how you might do this.


- (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 :

Because the only notification type supported for non-running applications is icon-badging, simply pass NSRemoteNotificationTypeBadge as the parameter of registerForRemoteNotificationTypes:.

关于ios - 仅在应用程序运行时接收报亭通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15186332/

相关文章:

ios - 为什么在 Android Chrome 和 Firefox 上运行 PWA 在 iOS Safari 上会变成白屏?

php - NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";

iphone - 为东部时区的 future 日期设置本地通知

ios - Flutter FCM iOS 问题 - 在检索 FCM token 之前未设置 APNS 设备 token

ios - UINavigationController 有时在 iOS5 上缺少返回按钮

ios - React-Native 更改 selectTextOnFocus 关键字(选择、全选、粘贴)

iOS 6 map 未显示提供的经纬度位置

ios - 向 nsdictionary 添加各种对象和键

正确对齐的iOS Header StackView?

ios - 如何在 Delphi FireMonkey 中发送 iOS 推送通知