在显示推送通知 VS 静默推送之前触发后台刷新的推送通知 [英] Push notifications that trigger a background refresh before showing the push notification VS silent push

查看:24
本文介绍了在显示推送通知 VS 静默推送之前触发后台刷新的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在收到推送时在我的应用中实现后台刷新功能.就在向用户显示推送通知之前,我想从我的后端 (Parse.com) 下载新消息并将它们保存到数组中.我正在遵循这里的指南:http://developer.xamarin.com/guides/ios/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/updating_an_application_in_the_background/

I want to implement background refresh functionality in my app for when a push is received. Just before the push notification is displayed to the user I want to download the new messages from my backend (Parse.com) and save them to an array. I am following a guide from here: http://developer.xamarin.com/guides/ios/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/updating_an_application_in_the_background/

我不确定本指南的准确性.它指出:iOS 7(及更高版本)通过让应用程序有机会在通知用户之前在后台更新内容来扩展普通推送通知,以便用户可以打开应用程序并被呈现立即添加新内容.

I'm not sure how accurate this guide is. It states: iOS 7 (and greater) extends ordinary push notifications by giving applications a chance to update content in the background before notifying the user, so that the user can open the application and be presented with new content immediately.

所以我尝试像这样实现我的后台推送:

So I tried implementing my background push like this:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{


    if([[userInfo objectForKey:@"aps"] objectForKey:@"content-available"]){

        NSLog(@"Doing the background refresh");
        UINavigationController *navigationController=(UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];

        MyViewController *myViewController = (MyViewController *)[[navigationController viewControllers] objectAtIndex:1];

        [myViewController.currentUser refreshMessagesArrayWithCompletionHandler:^(BOOL successful, BOOL newMiaos) {

            NSLog(@"messages refreshed the array now has %lu messages",(unsigned long)[myViewController.currentUser.messages count]);
            handler(UIBackgroundFetchResultNewData);
        }];
    }
}

调用后台刷新并显示推送,但是推送通知不会等待后台任务完成.它只是在收到后立即显示.这是正确的功能吗?上面的教程建议在后台任务完成之前不会显示通知.

The background refresh is called and the push is displayed, however the push notification does not wait for the background task to finish. It is just displayed as soon as it is received. Is this correct functionality? The tutorial above suggests that a notification won't be displayed until the background task is completed.

然后我开始尝试静默通知,这会触发应用程序在收到推送但未显示通知时在后台下载消息.所以我通过在下载完成后触发本地通知来做到这一点.这真的是正确的做法吗?whatsapp等传统应用程序是否使用静默通知触发后台刷新,然后触发本地通知?似乎有点hacky.当然,后台推送的想法是在显示通知之前准备好数据,但它并不完全像那样工作..

I then went about trying a silent notification, this triggers the app to download the messages in the background when the push is received but no notification is displayed. So I do this by firing a local notification instead after the download completes. Is this really the correct way of doing it? Do traditional apps such as whatsapp trigger a background refresh using a silent notification and then fire a local one? Seems a bit hacky. Surely the idea of a background push is to get the data ready before showing the notification but it doesn't quite work like that..

我注意到的另一件事是静默通知速率受限它们的优先级低于典型的推送通知,所以这肯定也会阻碍应用程序的效率......

The other thing I noticed is that silent notifications are rate limited they have a lower priority than a typical push notification so surely this hinders the efficiency of the app also...

对此的任何指示将不胜感激.只是想弄清楚我是否以正确的方式接近这个问题.一切似乎都很hacky...

Any pointers on this would be really appreciated. Just trying to get my head around if I'm approaching this the right way or not. All seems very hacky...

推荐答案

我一直在为我的 消息应用.我们希望用户在用户点击通知之前立即看到消息.我们面临的问题:

I've been struggling with the same task in my messaging app. We wanted users to see the message right before user taps the notification. What we faced with:

  • 有效载荷大小限制.iOS 7 的有效载荷只能有 256 个字节
  • 单个静默通知不会启动应用程序(如果应用程序未运行)
  • content-available 没有警报正文的通知甚至可能不会发送到设备
  • 后台提取不受您的应用程序控制,因此您可能永远不会收到所需的信号,因此我们不能依赖此功能.但这可能有助于实现我们想要的目标
  • iOS 8 有很大的负载空间 - 2KB
  • 如果您发送警报正文 content-available - 在大多数情况下它会被发送并且应用能够处理它
  • The payload size limitation. iOS 7 can have only 256 bytes for payload
  • single silent notifications will not start an app, if it is not running
  • content-available notifications without alert body may even not be delivered to the device
  • Background fetch is not controlled by your app, so you may never receive the desired signal, so we cannot rely on this feature. But this may be helpful as an additional way to achieve what we want
  • iOS 8 has a lot of space for payload - 2KB
  • if you send alert body and content-available - it will be delivered in most cases and an app is able to process it

所以我们得出了唯一可以接受的解决方案:我们决定仅在 ios8+ 中创建此功能.我们使用 content-available 键发送可见推送通知,这允许我们在进程正在运行/冻结时处理通知负载,并且在应用程序未运行时都能够显示通知.如果应用程序收到推送通知,它会将警报文本正文写入本地数据库,以便用户能够在对话中阅读它.根据我们的统计,消息的平均大小不超过 200 个符号,因此大多数时候不需要额外的请求.如果消息超过 200 个符号,我们会使用额外的参数扩展负载正文,该参数用于在推送通知处理中请求文本正文.用户将看到文本的裁剪版本,但在请求完成后,我们使用接收到的值在本地数据库中重写一条消息.

So we came to the only acceptable solution: we decided to make this feature in ios8+ only. We send visible push notifications with content-available key which allows us to process the notification payload if the process is running/frozen and both be able to present the notification if the app is not running. If an app receives push notification, it takes alert text body and writes it into local database, so the user is able to read it in conversation. According to our stats, the average size of the message is not more than 200 symbols, so most of the time no extra requests are required. If the message is longer than 200 symbols, we extend payload body with extra parameter which is used to request text body in push notification processing. The user will see a cropped version of text, but after a request is done, we rewrite a message in local database with the received value.

因此,该技术允许我们在大多数情况下立即向用户显示收到的消息 + 如果应用程序未运行,我们会在应用程序启动后立即向我们的服务器发出请求以获取丢失的消息.这是我们可以在 iOS 上获得的最快和最可接受的情况.希望我的经验能帮助你实现你想要的.

So, that technique allows us to show a received message to the user immediately in most of the cases + if the app was not running, we make a request to our server to fetch missing messages right after application start. This is the fastest and the most acceptable case we could get on iOS. Hope my experience will help you to implement what you want.

这篇关于在显示推送通知 VS 静默推送之前触发后台刷新的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆