3

I noob in development application on OSX. I want to create app with Share extension. After content loading I want to show Notification, but I getting error "Notifications are not allowed for this application". I don't understand why requestAuthorization method not show dialog windows with permission, and how I can allow application to send notifications.

This is my code:

import Cocoa
import UserNotifications

class ShareViewController: NSViewController {
    override func loadView() {
        self.view = NSView()

        // Insert code here to customize the view
        let item = self.extensionContext!.inputItems[0] as! NSExtensionItem
        NSLog("Attachment = %@", item.attachments! as NSArray)
        showNotification()
        let outputItem = NSExtensionItem()
        let outputItems = [outputItem]
        self.extensionContext!.completeRequest(returningItems: outputItems, completionHandler: nil)
    }

    func showNotification() -> Void {
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.requestAuthorization(options: [.alert, .badge]) {
            (granted, error) in
            if granted {
                print("Yay!")
            } else {
                print("D'oh") // Print this, not authorized
            }
        }
        let content = UNMutableNotificationContent()

        content.title = "Hello"
        content.body = "This is example"
        content.sound = UNNotificationSound.default
        content.badge = 1
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        notificationCenter.add(request) { (error : Error?) in
            if let theError = error {
                print(theError) // Print Domain=UNErrorDomain Code=1 "Notifications are not allowed for this application"
            }
        }
    }
}
2
  • Have you checked privacy panel if it's not already there? If it is and you want new dialog reset TCC (tccutil reset <service name>) or just call reset. Once I had issues and only reboot helped (conflict cause system preferences was opened on privacy pane and it run to race condition - i changed it programaticaly while it has been opened )
    – Marek H
    Oct 14, 2019 at 16:49
  • For me on iOS13/XCode11, I was having this problem. It turned out to be a race-condition. I added a solution below.
    – paiego
    May 12, 2020 at 0:38

2 Answers 2

1

I don't see anywhere in the documentation that says you can't schedule new Local Notifications from an extension. I do however see an apple support ticket where someone had to deal with this issue, as do I.

Basically this failure is a race-condition. The key is to not call the contentHandler of this extension method:

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

until after the completion handler of

notificationCenter.add(request: UNNotificationRequest>, withCompletionHandler: ((Error?) -> Void)

is called. It worked for me. Make sense?

1
  • > I don't see anywhere in the documentation I not sure that documentation describes this moment. I remember that see NS_EXTENSION_UNAVAILABLE macro opposite Notification Api in source code
    – rusnasonov
    May 12, 2020 at 6:26
-2

Apple does't allow send Notifications from Share extensions.

Documentation

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.