The application was opened from an icon or notification

My problem is this.

I send a request to the server with a request to notify me of an event that will occur in 2-10 minutes, and close the application, and the server will respond to this request through a push notification. Until the server responds with a notification, the application will show the animation, just in case, if it remains in the foreground.

When I receive a notification when the application is open, the animation should stop and display some data from the notification. My problem is that if I do not open the application from the notification (I open it from the icon), the animation continues to be displayed because the callback method for "didReceiveRemoteNotification" is never called.

Can I check if the application has opened from the icon or from the notification (local or push)?

+3
source share
3 answers

The paragraph application:didFinishLaunchingWithOptions:in the SDK explains very well which delegate methods are invoked in different start / wake up scripts (for example, clicking a registered URL handler, opening a supported mime type, responding to remote / local notifications, clicking on the icon on the main screen, etc.)

In addition, if your application is running at the time of the above events, they will cause a respective delegate methods (eg didReceiveRemoteNotification, openURLetc.), except applicationDidBecomeActive:. From a combination of called callbacks, you can figure out what event happened.

+2
source

, launchOptions. , .

- :

if(!launchOptions){
    NSLog(@"App invoked directly");
}
+1

AppDelegate applicationWillEnterForeground:

- (void) applicationWillEnterForeground: (UIApplication *) application {

// this method is called when staring an app that was closed / killed / never run before (after applicationDidFinishLaunchingWithOptions) and every time the app is reopened or change status from background to foreground (ex. returning from a mobile call or after the user switched to other app and then came back)

[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
    NSLog(@"AppDelegate-getDeliveredNotificationsWithCompletionHandler there were %lu notifications in notification center", (unsigned long)[notifications count]); 

    for (UNNotification* notification in notifications) {

        NSDictionary *userInfo = notification.request.content.userInfo;
        if (userInfo) {
            NSLog(@"Processed a notification in getDeliveredNotificationsWithCompletionHandler, with this info: %@", userInfo);
            [self showPushNotificationInAlertController:userInfo]; // this is my method to display the notification in an UIAlertController
        }
    }
    UIApplication.sharedApplication.applicationIconBadgeNumber = 0;

}];

}}

didFinishLaunchingWithOptions: , , :

UIApplication.sharedApplication.applicationIconBadgeNumber = 0;

This currently works in iOS 12, it was not possible to test it in earlier versions. In this method, you also need to implement code for processing notifications received when the application is in the foreground: willPresentNotification: (UNNotification *) notification using CompletionHandler: and this method for processing notifications in which the user opens your application by clicking on the notification: didReceiveNotificationResponse : (UNNotificationResponse *) answer withCompletionHandler:

0
source

All Articles