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

查看:128
本文介绍了在显示推送通知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个字节用于有效负载

  • 单个静默通知无法启动应用程序(如果未运行)

  • 内容可用没有警报正文的通知甚至可能无法发送到设备

  • 后台提取不受您的应用程序控制,因此您可能永远不会收到所需的信号,所以我们不能依赖这个功能。但这可能有助于实现我们想要的另一种方式

  • iOS 8有足够的空间用于有效载荷 - 2KB

  • 如果您发送警报正文 内容可用 - 在大多数情况下会提供,并且应用程序可以处理它

  • 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天全站免登陆