-->

iOS Push Notifications with empty aps dictionary

2020-08-16 17:10发布

问题:

Doing research to try and pick a direction for notification types. I'd like to be able to notify my app that there's new data to be refreshed but not bother the user with the popup/notification message. The idea is that the same notifications go out if the app is open or closed and when this "special" message arrives and the app is open it knows to fetch data.

My idea was to send an empty aps dictionary like example 5 at the bottom of this apple document.

My question is what will happen when this type of message is received? It says it'll clear the badge but will some sort of default message appear to the user? Or will this be completely silent?

Follow up question, is there some better way to do this other than checking if the app is running and telling my server to start sending "special" payloads (I'd like to handle everything through push)?

回答1:

If there is no badge, no alert, and no sound specified in the dictionary (for the "aps" key) then a default message will not appear and it will be completely silent.

Look again at example 5 in the document you referenced. aps can be empty, and you can specify whatever custom data you would like as they do with the "acme2" key. The "acme2" data is an example of where your server's "special" payload could reside within the JSON payload.

You don't need to tell the server that your app is running. The server can send the special payloads through APNS regardless if your app is running or not, and you will receive that special payload in one of two ways (assuming of course that the push does reach the device... which is not guaranteed):

  1. If your application is in the foreground then iOS will not intercept the notification. You will receive the notification in your app delegate's application:didReceiveRemoteNotification: method (provided that your app delegate does override the method).
  2. If iOS did intercept your push, then when you choose to launch your application in response to the notification then you will need to retrieve the "push dictionary" in your app delegate's application:didFinishLaunchingWithOptions: method as in the following example:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self application:application didReceiveRemoteNotification:[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
}