0

I am building a Newsstand app that I would like to have download the newest issue in the background. I have the appropriate background mode keys set to allow this.

I push a payload to my app, e.g.:

{"aps": {"badge": 1,"content-available":"1"}}

But if my application is not in the foreground, how do I know that the push notification was sent so that I can start the background download? If the app IS in the foreground, I use didReceiveRemoteNotification: but confused on what to do when the app is not in foreground or isn't even running.

According to the docs:

If an application is not running in the foreground when the notification is delivered, it is activated in the background (or launched into the background, if necessary) to download issue assets. Otherwise, you handle the notification as you would any push notification. See Local and Push Notification Programming Guide for information on how to send and handle push notifications.

The client application communicates with its server and gets URLs locating the issue assets to download. It might also need to obtain the name and date of the issue from the server. In this phase it might also validate that the user is eligible for a subscription or perform any other required authorization.

But it never says which method to use to start the download.

Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

2 Answers2

2

You app will only receive the push notification if the user launches the app in response to the push notification (e.g., slide it on the lock screen or from the notification center), or if it is already running. Also, push notifications are not guaranteed to be delivered.

Update:

As you mentioned, for Newstand apps, you should use the UIRemoteNotificationTypeNewsstandContentAvailability permission to enable background downloading of new issues. However, that is not without it's own set of technical challenges:

"...your app may only initiate a background download once every 24 hours. iOS will ignore notifications that try to initiate a background download more than once in a 24 hour period."

and

Can users disable background downloading?

Yes, users can disable Newsstand's push notification-initiated background downloading on a per-app basis in Settings. You can determine whether background downloading has been disabled for your app by looking for UIRemoteNotificationTypeNewsstandContentAvailability in the set of enabled types returned by UIApplication's enabledRemoteNotificationTypes property.

from: http://developer.apple.com/library/ios/#technotes/tn2280/_index.html

It is entirely possibly that during your testing that you are sending too many content updates throughout the day.

Community
  • 1
  • 1
Jason Whitehorn
  • 13,585
  • 9
  • 54
  • 68
  • Is this really true for `Newsstand Kit`? I just watched one of the Apple developer videos and it said to not send a message with the APN payload. Using `UIRemoteNotificationTypeNewsstandContentAvailability` should allow me to not have this limitation as far as I understand. – Nic Hubbard Dec 19 '12 at 20:59
  • @NicHubbard I've attempt to update my answer is response to that. Hopefully that helps. – Jason Whitehorn Dec 19 '12 at 21:16
  • But, I have set the `NKDontThrottleNewsstandContentNotifications` NSUserDefault which turns off the 24 hour limitation, only for development purposes. – Nic Hubbard Dec 19 '12 at 21:59
1

"You app will only receive the push notification if the user launches the app in response to the push notification (e.g., slide it on the lock screen or from the notification center), or if it is already running."

The above statement is not correct in case of Newsstand notification (having content-available:1 in payload). On receiving it iOS launch the app in background (if not running) and didFinishLaunchingWithOptions method of your app delegate is get called. You can check the value for UIApplicationLaunchOptionsRemoteNotificationKey of launch options dictionary to check if you application is launched by notification.

NSDictionary *payload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];    


    if(payload && [[payload objectForKey:kContentAvailablePush] caseInsensitiveCompare:@"1"] == NSOrderedSame)
    {
        NSLog(@"app launched by Newsstand Remote notification. payload %@", payload);
        [self scheduleNewsStandDownload:payload];        
    }

In method scheduleNewsStandDownload you can simply get the hosted content's path/date etc and add it in the Newsstand queue. Below is the pseudo code.

NKLibrary *nkLib = [NKLibrary sharedLibrary];
NKIssue *nkIssue = [nkLib issueWithName:<your content id>];
if(!nkIssue)
   nkIssue = [nkLib addIssueWithName:<your content id>  date:<your content date>];


    NKAssetDownload *nkAssetDownloadGridCover = [nkIssue addAssetWithRequest:<urlRequest for the content>];        
    [nkAssetDownloadGridCover downloadWithDelegate:self];
msk
  • 8,885
  • 6
  • 41
  • 72