2

私は OSX 上の開発アプリケーションに慣れていません。Share extensionでアプリを作成したい。コンテンツの読み込み後にNotificationを表示したいのですが、「このアプリケーションでは通知は許可されていません」というエラーが表示されます。requestAuthorizationメソッドが許可されたダイアログ ウィンドウを表示しない理由と、アプリケーションが通知を送信できるようにする方法がわかりません。

これは私のコードです:

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"
            }
        }
    }
}
4

2 に答える 2

1

ドキュメントのどこにも、拡張機能から新しいローカル通知をスケジュールできないという記述はありません。しかし、私と同じように、誰かがこの問題に対処しなければならなかったアップルのサポート チケットが表示されます。

基本的に、この失敗は競合状態です。重要なのは、この拡張メソッドの contentHandler を呼び出さないことです。

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

の完了ハンドラのまで

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

と呼ばれます。それは私のために働いた。わかる?

于 2020-05-12T00:36:11.827 に答える