0

I'm building a react-native app. Every time our cloud sends an APNS message, we want to increment the badge by 1.

I'm using the react-native PushNotificationIOS module to handle messages, but there's a known limitation with react-native, where the JS bridge is deactivated when the app is in the background. As a result, react-native application code that wants to manipulate the badge isn't executed, because the notification event is never delivered to the javascript.

It's important that this badge operation occur even when the app is not in the foreground, so I decided to implement the badge-incrementing logic in Objective-C, directly in AppDelegate.m. (Note: I don't understand ObjC, and can only loosely follow it.)

Here's the entirety of didReceiveRemoteNotification from AppDelegate.m:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
    // --- BEGIN CODE I ADDED --- //
    // increment badge number; must be written in ObjC b/c react-native JS bridge is deactivated when app in background
    NSLog(@"remote notification LINE 1");
    NSUInteger currentBadgeNum = [UIApplication sharedApplication].applicationIconBadgeNumber;
    NSLog(@"currentBadgeNum = %d", currentBadgeNum);
    NSUInteger newBadgeNum = currentBadgeNum + 1; // to increment by .badge: ... + [[[notification objectForKey:@"aps"] objectForKey: @"badge"] intValue]
    NSLog(@"newBadgeNum = %d", newBadgeNum);
    [UIApplication sharedApplication].applicationIconBadgeNumber = newBadgeNum;
    NSLog(@"done updating badge; now notifying react-native");
    // --- END CODE I ADDED --- //

    // forward notification on to react-native code
    [RCTPushNotificationManager didReceiveRemoteNotification:notification];
}

When I test it, none of the log statements appear in Xcode when the app is backgrounded. However, when the app is active, the log statements appear and the routine works exactly as expected (and the notification is delivered to the javascript code as well).

I believe I've already configured the app to receive push-notifications; here's the relevant section from my Info.plist:

<key>UIBackgroundModes</key>
<array>
  <string>remote-notification</string>
</array>

What am I missing? Is there a problem with my code, or am I misunderstanding the nature of didReceiveRemoteNotification?

Any help is appreciated.

Tom
  • 8,509
  • 7
  • 49
  • 78
  • 1
    Your code only runs when your app is run. If the push comes through and they ignore the message, your badge can't auto-increment. Your server side has to keep track and include the 'badge' key in the 'aps' packet. When your app starts, and you register with your server, then the server should decrement the count back to 1. – Gargoyle Jun 03 '16 at 22:20
  • @Gargoyle: you should probably submit that as an answer. If it turns out you're correct, I'll need to be able to mark it. Either way, I want to wait a few more days in case there's another answer out there. – Tom Jun 06 '16 at 15:33
  • I thought that `remote-notification` setting was designed to allow the app a chance to wake up and download information from the web in response to a push notification. I'm looking at this SO q: http://stackoverflow.com/questions/22090989/background-task-when-apns-received – Tom Jun 06 '16 at 15:38
  • Could be. let me know what you find out! – Gargoyle Jun 07 '16 at 04:17
  • @Gargoyle: looks like you're right. It's bizarre to me, but whatever. Apple's ecosystem. – Tom Jul 05 '16 at 15:56

0 Answers0