4

I am creating a chat messenger app like whatsapp and trying to implement notification functionality similar to whatsapp. In whatsapp when you receive a notification it stores the data somewhere and when you turn off your wifi and go into the app the message is injected in the application. This mean whatsapp is somehow accessing the notification even when application is closed.

My Approach: I am receiving notification in background mode and saving that into a file. So if the user gets disconnected from the internet and goes into the app the messages are still injected on applicationWillAppear. This works perfect but when you forcefully close your app (Double clicking home and swiping the app up) it does not work. I have search almost everything and it says background fetch will not work if you forcefully close your application.

Then how whatsapp is doing it? What can be any other solution?

My Solution:

Turned on Background Modes for Background Fetch and remote notification from XCode. Next added following code inside application:willFinishLaunchingWithOptions:

[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

Added this in AppDelegate.m file

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler{

  NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);

  if (0 < [paths count]) {
    NSString *documentsDirPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirPath stringByAppendingPathComponent:@"notification.txt"];

    NSString *content = [NSString stringWithFormat:@"%@", userInfo];
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
      // Append text to the end of file
      NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:filePath];
      [fileHandler seekToEndOfFile];
      [fileHandler writeData:data];
      [fileHandler closeFile];
    } else {
      // Create the file and write text to it.
      [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
  }

  handler(UIBackgroundFetchResultNewData);

}

Update: I do have following flag in my notification

content-available: 1
Nakib
  • 4,593
  • 7
  • 23
  • 45
  • Why do you need a background fetch if you already save the data to file ? – Teja Nandamuri Jun 09 '16 at 17:40
  • Research abt silent push notification. – Prajeet Shrestha Jun 09 '16 at 17:42
  • @TejaNandamuri data is saved only when that function is called. But its not called when you forcefully close your application. – Nakib Jun 09 '16 at 17:46
  • this could help https://www.quora.com/How-does-WhatsApp-or-Gmail-receive-notification-even-after-force-closing-them. Look at the discussions. – Teja Nandamuri Jun 09 '16 at 17:47
  • @PrajeetShrestha that what i am doing. It does work in background but not when its forcefully closed. – Nakib Jun 09 '16 at 17:48
  • did u try this :http://stackoverflow.com/questions/12116282/handling-push-notifications-when-app-is-not-running – Teja Nandamuri Jun 09 '16 at 17:50
  • @TejaNandamuri thats completely different scenario. Its what to do when app is launched by clicking on notification. – Nakib Jun 09 '16 at 17:51
  • If you force close your application, you cannot do background fetch. You have to restart the app and do the fetch again! – Teja Nandamuri Jun 09 '16 at 17:55
  • For a silent notification, take care to ensure there is no alert, sound, or badge payload in the aps dictionary. If you don’t follow this guidance, the incorrectly-configured notification might be throttled and not delivered to the app in the background, and instead of being silent is displayed to the user. – Muhammad Zohaib Ehsan Jun 09 '16 at 18:04

1 Answers1

3

Background pushes do not get delivered to a user-terminated app.

Unless it is a voip push, in that case the app will be started by the OS if necessary (but yes you can only make use of voip push if your app provides voip functionality to the user.)

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • when you say "started by OS " , does this require additional permission to get from OS for our app ? Is it how the Facebook,whatsaapp do ? – Teja Nandamuri Jun 09 '16 at 17:56
  • @the-pumping-lama whatsapp does it without starting the app. – Nakib Jun 09 '16 at 18:02
  • 1
    You have to add a background mode capability of voip to the app and it uses a voip services certificate which is separate from a regular ssl push certificate. Voip push notifications are not implemented in the app the same way as a regular push, they are done using push kit. – Gruntcakes Jun 09 '16 at 18:03
  • I will have a look at push kit and voip push notification – Nakib Jun 09 '16 at 18:05
  • "started by the OS " doesn't mean the app goes into the foreground and is visible to the user. – Gruntcakes Jun 09 '16 at 18:07
  • yea got it.it runs as a service – Nakib Jun 09 '16 at 18:09
  • Its launched into the background state. iOS doesn't have the equivalent of a service like Android does. – Gruntcakes Jun 09 '16 at 18:11
  • But the question is voip push using push kit look designed for voip calling feature and we use it for message to handle scenario even after application terminate. This may lead apple to reject app at AppStore, can you please clearify.., – Ravi Kumar Jan 11 '17 at 18:48
  • 1
    @Ravi H Malviya, yes you can only make use of voip push if your app provides voip functionality to the user. – Gruntcakes Jan 18 '17 at 16:49