1

I'm trying to send a push notification to my app using pushy.me. When the app is in the background I'm unable to do anything with the data received, unless I tap on the Notification Banner or the app is in the foreground..

Here is my Notification Payload

   {
  "registration_ids": [
  "692d1e6c16d483a4613193"
   ],
   "data": {
   "title": "Test Notification",
   "message": "hello"
  },
  "notification": {
    "body": "Hello World ✌",
    "badge": 1,
    "content-available" : "1",
   "mutable-content" : "1",
    "sound": "ping.aiff"
  }
}

Code:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{

    NSLog(@"%@", pushInfo);

    if(application.applicationState == UIApplicationStateInactive) {

        NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");
        //do some tasks
      //  [self manageRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
    }
    else if (application.applicationState == UIApplicationStateBackground) {
        //NOT WORKING
        NSLog(@"application Background - notification has arrived when app was in background");
        NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[pushInfo valueForKey:@"aps"] valueForKey:@"content_available"]];


        if([contentAvailable isEqualToString:@"1"]) {
            // do tasks


        //    [self manageRemoteNotification:userInfo];
            NSLog(@"content-available is equal to 1");
        //    completionHandler(UIBackgroundFetchResultNewData);
        }
    }
    else {
        NSLog(@"application Active - notication has arrived while app was opened");
        //Show an in-app banner
        //do tasks
     //   [self performFetchWithCompletionHandler];
        completionHandler(UIBackgroundFetchResultNewData);
    }
}
Edward Tattsyrup
  • 245
  • 1
  • 3
  • 15
  • Your `(application.applicationState == UIApplicationStateBackground)` branch won't be executed when app is backgrounded, but when user actually taps on the notification banner while the app was backgrounded. – Kamil.S Jun 24 '19 at 12:21
  • Consider sending the `content_available: true` parameter to send background notifications that wake your app and execute its `didReceiveRemoteNotification`: https://pushy.me/docs/api/send-notifications – Elad Nava Aug 08 '19 at 02:27

0 Answers0