I’m trying to write an app that basically serves as a countdown timer. I’ve
learned that the WKInterfaceTimer object doesn’t really do anything but
draw the countdown on the screen. The documentation suggests pairing it
with an NSTimer in order to be able to do something when the timer expires.

I think the NSTimer approach is pretty worthless on the watch, because if
the timer counts down for a long time, the app may be suspended or killed.
And even if it only goes to background, when the timer expires, you’re not
allowed to play haptics to alert the user. (These are the first things I
tried, to no avail.)

So instead of using a timer, I schedule a notification. The problem is, I
can’t seem to make sure the notification’s alert is displayed when it
arrives!

Below is the top part of my ExtensionDelegate file. When the notification
arrives, I get the message on my console saying we’re asking the system to
present it, but nothing happens.

What the heck else do I need to do to make sure notifications aren’t
suppressed by the watch?

import WatchKit
import UserNotifications

class ExtensionDelegate: NSObject, WKExtensionDelegate,
UNUserNotificationCenterDelegate {

  func applicationDidFinishLaunching() {
    let nc = UNUserNotificationCenter.current()
    nc.delegate = self
    nc.requestAuthorization( options: [.alert, .sound] ) {
      ( accepted, error ) in
      if !accepted {
        print("Notification access denied.")
      }
    }
  }

  func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (
UNNotificationPresentationOptions ) -> Void
  ) {
    print( "Received alert--asking system to present it" )
    completionHandler( [ .alert, .sound ] )
  }

  <snip>
}

-- 

Charles
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to