Handling Push Notifications when App is NOT running

后端 未结 8 2152
感动是毒
感动是毒 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:13

    When your app is not running or killed and you tap on push notification this function will trigger;

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

    so you should handle it like this,

    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (localNotif) {
        NSString *json = [localNotif valueForKey:@"data"];
        // Parse your string to dictionary
    }
    
    0 讨论(0)
  • 2020-11-29 17:14

    Try going to the didReceiveRemoteNotification method and handling it there. In there you can check for the application states by doing conditionals for the applicationState, for example checking if it's UIApplicationStateActive or others.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 17:23

    try this

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
    
        //register notifications
        if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) // ios 8
        {
            [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
            [application registerForRemoteNotifications];
        }
        else // ios 7
        {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge];
        }
    
        // open app from a notification
        NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
        if (notification) 
        {
            //your alert should be here
        }
    
        // Set icon badge number to zero
        application.applicationIconBadgeNumber = 0;
    
        return YES;
    }
    
    0 讨论(0)
  • 2020-11-29 17:25

    I tried @dianz's answer, It's not worked for me but I got a help from the answer.

    In my case;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSDictionary *apnsBody = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (apnsBody) {
        // Do your code with apnsBody
    }
    
    0 讨论(0)
  • 2020-11-29 17:31

    Better be if you override didReceiveRemoteNotification Here you go

    NSDictionary * pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(pushNotificationPayload) {
        [self application:application didReceiveRemoteNotification:pushNotificationPayload];
    }
    

    This will again fire the didReceiveRemoteNotification method and your push notification code will execute as same as it runs during application run time.

    0 讨论(0)
提交回复
热议问题