Handling Push Notifications when App is NOT running (i.e Totally Killed)

前端 未结 5 1326
孤城傲影
孤城傲影 2021-01-07 07:16

I\'m able to send push notifications to my IOS device. But when I click on that notification it just opens the app. No message is shown inside the app.

Code used by

5条回答
  •  不知归路
    2021-01-07 07:25

    Handling Push Notifications when App is NOT running (or Totally Killed)

    I'm posting this solution as it worked for me.

    Go to your AppDelegate.m file.

    Step 1: Write this code inside this function:

    -(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    {
    
      UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
     if (localNotif) {
    
            NSString *cancelTitle = @"Close";
            NSString *showTitle = @"OK";
            NSString *message = [[localNotif valueForKey:@"aps"] valueForKey:@"alert"];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
                                                                message:message
                                                               delegate:self
                                                      cancelButtonTitle:cancelTitle
                                                      otherButtonTitles:showTitle, nil];
            [alertView show];
    
    
        }
    
    }
    

    Step 2:

    Insert This full code:

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    {
    
    NSLog(@"%s..userInfo=%@",__FUNCTION__,userInfo);
    
    /**
         * Dump your code here according to your requirement after receiving push
         */
    
        if (application.applicationState == UIApplicationStateActive) {
            NSString *cancelTitle = @"Close";
            NSString *showTitle = @"OK";
            NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
                                                                message:message
                                                               delegate:self
                                                      cancelButtonTitle:cancelTitle
                                                      otherButtonTitles:showTitle, nil];
            [alertView show];
    
        } 
    
        else if(application.applicationState == UIApplicationStateBackground){
    
            //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
    
    
            NSString *cancelTitle = @"Close";
            NSString *showTitle = @"OK";
            NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
                                                                message:message
                                                               delegate:self
                                                      cancelButtonTitle:cancelTitle
                                                      otherButtonTitles:showTitle, nil];
            [alertView show];
    
        }
    
    
        else if(application.applicationState == UIApplicationStateInactive){
    
            //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
    
    
            NSString *cancelTitle = @"Close";
            NSString *showTitle = @"OK";
            NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received"
                                                                message:message
                                                               delegate:self
                                                      cancelButtonTitle:cancelTitle
                                                      otherButtonTitles:showTitle, nil];
            [alertView show];
    
        }
    
    
    }
    

    This whole code will work whether app is Active, InActive or Totally Killed. It will give you AlertView for push messages.

提交回复
热议问题