问题
I'm implementing newsstand features in an application and though the app receives the push notification it does not start in background mode.
If I tap the notification alert the the app starts and I can see "content-available":1 is present in the dictionary and also the issue is downloaded, but the app is not automatically launched.
I've added to the plist:
<key>UIBackgroundModes</key>
<array>
<string>newsstand-content</string>
</array>
and to didFinishLaunchingWithOptions:
[[NSUserDefaults standardUserDefaults]setBool: YES forKey:@"NKDontThrottleNewsstandContentNotifications"]; // for testing purposes
[[NSUserDefaults standardUserDefaults] synchronize];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeNewsstandContentAvailability )];
I also see that my app is not shown under Settings -> Store -> Automatic Downloads (and other magazines appears there).
Am I missing something? Is this supposed to work in sandbox environment?
回答1:
Some clarifications
- If you do not send "alert" in your Newsstand payload which has only content-available:1 in it, nothing will get added in notification center.
- Newsstand notification launch application does not mean the app will come to foreground (as in if user tapped over app icon). It just mean that if the app is not in background, it will get launched by iOS in background -> didFinishLaunchingWithOptions of appDelegate is called, where app should check if it is a Newsstand notification to schedule the download by adding assets in Newsstand queue. The asset paths can be part of NS payload (provided < payload limit 256 bytes)
NSDictionary *payload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(payload && [[payload objectForKey:kContentAvailablePush] caseInsensitiveCompare:@"1"] == NSOrderedSame) { NSLog(@"Launched due to NS notification"); }
回答2:
You have to register for newsstand notification in order to appear in Settings and receive the "newsstand notifications". To register, add this to your application:didFinishLaunchingWithOptions:
:
// Add registration for newsstand notifications
// In your application:didFinishLaunchingWithOptions:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeNewsstandContentAvailability];
The user will be asked to accept background download or not.
Take a look at this really complete tutorial about newsstand apps : http://www.viggiosoft.com/blog/blog/2011/10/17/ios-newsstand-tutorial/
回答3:
Make sure, that UINewsstandApp = YES was set in your plist
回答4:
{"aps": {"badge": 1, "alert": "test","content-available":1}}
This is a right payload.
{"aps": {"badge": 1, "alert": "test"},"content-available":1}
This is a wrong payload.
回答5:
This is what happens when content-available is in the payload:
- If app is Suspended, the system will bring it into Background
- If app was killed by user, nothing happens and app remains in Not Running
There has to be an user action to start the application essentially by adding the alert message into the push notification.
source
http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/
However this doesn't solve your problem. As workaround you can use the background fetch, which wakes up the application every certain time interval.
来源:https://stackoverflow.com/questions/12218073/ios-newsstand-push-notification-does-not-launch-the-app-in-background