• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Make WatchKit app show local notification
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Make WatchKit app show local notification


  • Subject: Make WatchKit app show local notification
  • From: Charles Jenkins <email@hidden>
  • Date: Thu, 10 Nov 2016 20:50:52 -0500

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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Make WatchKit app show local notification
      • From: Jeff Kelley <email@hidden>
  • Prev by Date: MPMediaPickerController - hide explicit songs?
  • Next by Date: Re: Make WatchKit app show local notification
  • Previous by thread: MPMediaPickerController - hide explicit songs?
  • Next by thread: Re: Make WatchKit app show local notification
  • Index(es):
    • Date
    • Thread