iOS Click Notification Bar Notification Enter Program Trigger Event

  • 2021-10-25 08:05:26
  • OfStack

When clicking the notification in the notification bar to enter the program, the methods in App Delegate will be triggered, which can be divided into the following two situations:

1. When the program is not started (that is, there is no process in the bottom taskbar):

In this case, clicking the notification in the notification bar to enter the program will trigger the following method


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

For the launchOptions parameter, the following processing is required:


NSDictionary* pushNotificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

Among them, pushNotificationKey is the data pushed from the server, and the extra parameters passed by the server can be read by using key-value pairs.

The application has not been loaded, and if you click the display button of the notification, didFinishLaunchingWithOptions will be called instead of didReceiveRemoteNotification method. If you click the Close button of the notification and then click Apply, only the didFinishLaunchingWithOptions method will be called.

2. The program has started:

If the program has been started and resides in memory, whether the program is in the foreground (Foreground) or in the background (Background), clicking on the notification bar to enter the program will trigger the following methods:


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

The userInfo parameter is already the data pushed by the server, and it is also read by key-value pair.

If you click Close and then click Apply, the above two methods will not be called. At this time, only in applicationWillEnterForeground or applicationDidBecomeActive, you can judge whether there is a notification according to badge in the notification sent, and then send a request to obtain data


Related articles: