ios handle pushnotification in background

2019-08-09 02:51发布

问题:

I want to save a push notification that arrives when the application is in background state. I know about

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

This delegate method is called when the app is in foreground or background (if the user just clicks on the received notification).

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

This method is called only once when the app starts.

What if the user doesn't click on the notification? How can I save the data of that notification in the database of my app in that case?

回答1:

For this case, you can't save the data in the 'database of [your] app'; instead you save the data in the database on your server, which is where the PushNotification was generated in the first place. Then, when your App is started, it queries your database to get the notification data.



回答2:

If the user doesn't click on the notification, you can't save that data. Even if the user opens the app by tapping its launch icon the app won't receive that data.

Also remember the design of notification is to deliver small payload to alert the user. Once the users starts the app, the app can download the real data you want to save.