gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:10:24 24 4
gpt4 key购买 nike

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

该应用具有适当的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com