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"
            }
        }
    }
}
rusnasonov
  • 752
  • 2
  • 12
  • 23
  • Have you checked privacy panel if it's not already there? If it is and you want new dialog reset TCC (tccutil reset ) 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 '19 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 '20 at 00:38

2 Answers2

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?

paiego
  • 3,619
  • 34
  • 43
  • > 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 '20 at 06:26
-2

Apple does't allow send Notifications from Share extensions.

Documentation

rusnasonov
  • 752
  • 2
  • 12
  • 23
  • This statement is false. See https://developer.apple.com/documentation/usernotifications/unusernotificationcenter?language=objc which explains that you can use the UNUserNotificationCenter from an App extension. – ciceron Jun 12 '20 at 11:55