0

In my app I currently update the application badge after certain events using this code which works:

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

However [UIApplication sharedApplication] cannot be called from WatchKit or the Widget. Is there a way for my widget to update the application badge by some other way?

I tried using a background fetch like this. The badge gets updated when I explicitly "Simulate Background fetch" from Xcode. But on the actual device the background fetch never gets called? Another idea is a command to call the background fetch?

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // Background Fetch
    [[GlobalNotifications sharedInstance] setApplicationBadge];
    completionHandler(UIBackgroundFetchResultNewData);
}

Thanks for any insight on this.

1 Answers1

0

You are correct that scheduleLocalNotification can only be called from the iOS main app. For the watch app, if you are using watchOS 2 you can tell the iOS app to schedule the notification in the background using WatchConnectivity. I would recommend using the WCSession method sendMessage to schedule the notification.

In watchOS 1, you can use the openParentApplication method as so:

// On the watch extension
[InterfaceController openParentApplication:request reply:^(NSDictionary *replyInfo, NSError *error) {
    // handle response from phone
}];

// In the AppDelegate of the phone:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply 
    // TODO: Schedule notification here...
    NSDictionary *response = // Your custom response
    reply(response);
}

I am not aware of a way for a different kind of app extension (like a today extension) to do this kind of communication with the parent app.

lehn0058
  • 19,977
  • 15
  • 69
  • 109
  • Thanks again lehn0058. My app is using watchOS 1. Also what about the widget? Is there anyway to do this with the widget and/or watchOS 1? Thanks. – Basri Hassan Jan 28 '16 at 15:47
  • I updated my answer to include watchOS 1. I am not aware of a way to do this with a different kind of extension though. – lehn0058 Jan 28 '16 at 15:57
  • Thanks lehn0058. So that means a user cannot update the badge from the widget? (maybe somebody else knows?) I will try this for the watch OS 1 and post my findings. Thanks. – Basri Hassan Jan 28 '16 at 16:53
  • Any comment on the background fetch lehn0058? In Xcode when I simulate the background fetch it works as expected but on the actual device nothing happens. Unless there is a way to force a background fetch to happen? – Basri Hassan Jan 28 '16 at 16:58
  • For a background fetch to work, the iOS app needs to be open in the background. When you do a background fetch with Xcode the app needs to be running for the debugger to be attached. Are you keeping the app opened in the background when you are testing it on the actual device without Xcode attached? – lehn0058 Jan 28 '16 at 17:01