Handling Push Notifications when App is NOT running

后端 未结 8 2169
感动是毒
感动是毒 2020-11-29 16:57

When my App is not running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn\'t prompt the

8条回答
  •  死守一世寂寞
    2020-11-29 17:21

    I had the same problem but I finally could handle Push Notification when the is Inactive (not running) using Notification Service Extension. This solution was recommended by Apple Team Support and it is only for iOS 10, I implemented it and it works well! I leave the link:

    https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ModifyingNotifications.html

    This is the procedure:

    1. Create a new Notification Service Extension for your app. Open your project and press File - New - Target and select on iOS section, the option "Notification Service Extension". Put name of extension and the info that is required. After that, Xcode will added two files with Objective C language: the .h and .m file.

    NOTE: Consider that you will use three identifiers: 1) Identifier for your app (you already have) 2) Identifier for your extension (you will create) 3) Identifier for App Group. (you will create)

    1. Is necessary to enable App Groups to create a resource where you can save and read info for your app and the extension. Click on "Show Project Navigator". Into the list of targets select your main project. Then, click on "Capabilities" and switch on the option named "App Groups". Please add the identifier for App Group to identify share resources. You should do the same step for the target of extension that you created (Select target of extension - Capabilities - Switch on at "App Groups" - Add the same identifier for App Group)

    2. You should to add the Identifier for your extension into Apple Developer Site for identify the Notification Service Extension, also you should to make new Provisional Profiles (Development, AdHoc and/or Production) and associate it with the new Provisional Profiles.

    3. On both identifiers (App and Extension) you should edit them and enable "App Groups" Service in both of them. You should to add the Identifier for App group into the App Groups Services.

    NOTE: Identifier for App and Identifier for Extension SHOULD HAVE THE SAME IDENTIFIER FOR APP GROUP.

    1. Download the new Provisional Profile on Xcode and associate them with your Notification Service Extension. Please ensure you everything is ok with them.

    2. After that, into "Capabilities" of your app and extension, open "App Groups" section and update them. The three steps - 1)Add the App Groups entitlements to your entitlements file, 2) App the App Groups feature to your App ID and 3) Add App Groups to your App ID - should be checked.

    3. Come back to Project navigator and select the folder of you extension. Open the .m file. You will see a method called didReceiveNotificationRequest:(UNNotificationRequest *)request. Into this method you will create a different NSUserDefaults with SuiteName exactly equal to the Identifier for app group like this:

      NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"identifier for app group"];

    4. Into this same method, get the body of the notification and save it into a NSMutableArray, then save it in the share resources. Like this:

    - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler{
        self.contentHandler = contentHandler;
        self.bestAttemptContent = [request.content mutableCopy];
    
        NSMutableArray *notifications = [NSMutableArray new];
        NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"Identifier for app group"];
        notifications = [[defaultsGroup objectForKey:@"notifications"] mutableCopy];
        if (notifications != nil){
            [notifications addObject:self.bestAttemptContent.userInfo];
        }else{
            notifications = [NSMutableArray new];
            [notifications addObject:self.bestAttemptContent.userInfo];
        }
        [defaultsGroup setObject:notifications forKey:@"notifications"];    
    }
    
    1. Finally, into the App Delegate of your main project, in your didFinishLaunchingWithOptions method, recover the Array into the share resources with this code:
    NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"Identifier for app group"];
    NSMutableArray *notifications = [[defaultsGroup objectForKey:@"notifications"] mutableCopy];
    
    1. Enjoy it!

    I hope to be clear with each step. I going to write a post to implement this solution with images in my page. Another point that you should consider is it doesn't work with Silent Push Notification.

提交回复
热议问题