2

As I can read in guideline notifications are displayed automatically. But.. not for me. The question is why? My iOS app send notification after 4 seconds when user tap the button:

@IBAction func stopTimerPress(sender: AnyObject) {
        print("Stop timer!")
        notification.alertBody = "STOP!"
        self.timer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: "fire", userInfo: nil, repeats: true)
}

and then I expect that (if the app in iOS is in background) my Apple Watch display static notification. But it did not happen. What can I do? I uncommented this:

override func didReceiveLocalNotification(localNotification: UILocalNotification, withCompletion completionHandler: ((WKUserNotificationInterfaceType) -> Void)) {
        print(localNotification.alertBody)
        completionHandler(.Default)
    }

in NotificationController but this still didn't work. Can someone help me?

Stefan
  • 5,203
  • 8
  • 27
  • 51
Mr. A
  • 704
  • 1
  • 7
  • 16

2 Answers2

1

Have you requested permission from the user to view notifications on the iOS app? If the iPhone screen is off (not just the app in the background) then it should forward the notification to the Apple Watch.

UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

Also, if the Apple Watch app is in the foreground and the screen is on when the notification arrives, then it won't display in the UI, but instead will call the didReceiveLocalNotification method. If the screen is off, then this method won't get called but the notification will appear as a usual notification.

lehn0058
  • 19,977
  • 15
  • 69
  • 109
  • Probably I forgot about Alert notification type. Maybe this is the reason? – Mr. A Nov 09 '15 at 15:49
  • That could certainly cause the notification to not appear. – lehn0058 Nov 09 '15 at 15:50
  • Ok. Just one thing - what I should do in WatchKit side? – Mr. A Nov 09 '15 at 16:00
  • On the WatchKit sidethere is a didReceiveLocalNotification method you can implement on your ExtensionDelegate. This will get called when the app is running and the watchkit screen is on (and iPhone screen is off). – lehn0058 Nov 09 '15 at 17:33
  • Will this work on real device or it can be tested through simulator? – Ashok Nov 16 '16 at 09:20
  • Local notifications will show up on the simulator. The simulator is not able to register for push notifications. – lehn0058 Nov 16 '16 at 13:13
  • @lehn0058 I have a scenario where I am not receiving my local notifications on watch side in case if app is in background and screen is not turned on. Is it fine? – G.Abhisek Sep 05 '17 at 13:26
0

When you're in the background, your code is not running. However, If you click on your project in Xcode, and go to "Capabilities" and then into "Background Modes" and click "Remote Notifications." Then your app will be awakened at times known only to Apple, and then your code will be run provided it lasts no more than 30 seconds. You can use the Xcode menu item "Simulate Background Fetch" in the Debug menu to send a fetch message when your app is being debugged, to test.

Note that this may or may not run your code in 4 seconds - as Apple decides when to wake up your background app. I've seen it wake up every 5 minutes, or even after a couple of hours.

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
  • Ok, but why the notification wasnt show on Apple Watch? Even if app run on foreground – Mr. A Nov 09 '15 at 15:37
  • If the app shows up on the phone, then it won't show up on the watch. You need to have the phone locked to get a notification to show up on the watch. Apple's logic is (I believe) that if you're looking at your phone, you'll get it on your phone. If your phone is locked, you'll get it on the watch. – Owen Hartnett Nov 09 '15 at 18:53
  • Note that your phone needs to be locked (which means you can see the lock screen, or the phone is off (not completely powered off, the kind of off you get when hitting the side button or when the lock screen vanishes). – Owen Hartnett Nov 09 '15 at 18:56