2017-02-19 6 views
-1

時計アプリから時計キット通知をトリガするためにどのようなコードを書くのですか?たとえば、ウォッチストーリーボードからWatchInterfaceControllerにボタンを接続すると、そのボタンを押すとウォッチの通知がトリガーされます。WK通知をトリガーする方法

答えて

0

時計の通知をテストするには、まず新しいビルド方式を作成する必要があります。

あなたの時計アプリのスキームを複製し、「実行」セクションでカスタム通知を実行可能ファイルとして選択します。

これで通知スキームを実行できます。

プロジェクト内の拡張グループの中で、[サポートファイル]の下には、PushNotificationPayload.jsonというファイルがあります。

ペイロードファイルを編集して、さまざまな通知とカテゴリを試すことができます。トリガの

+0

ありがとう:通知は、あなたがこのような機能を使用することができますトリガについて それが引き起こされる? –

+0

それはできません。 Pusherのようなリモート通知プロバイダを使うべきです(これは実装が簡単です)。そのため、ウェブインターフェースにアクセスしてプッシュ通知を送信すると、すべての「購読済み」端末に表示されます。しかし、ボタンなどでトリガすることはできません。 –

0

あなたが許可を必要とするすべての最初の通知、:(ExtensionDelegate通常宣言)

func askPermission() { 

    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert,.sound]) { (authBool, error) in 
     if authBool { 
      let okAction = UNNotificationAction(identifier: "ok", title: "Ok", options: []) 
      let category = UNNotificationCategory(identifier: "exampleCategoryIdentifier", actions: [okAction], intentIdentifiers: [], options: []) 

      UNUserNotificationCenter.current().setNotificationCategories([category]) 
      UNUserNotificationCenter.current().delegate = self 
     } 
    } 
} 

この作業を持つために、あなたは(ExtensionDelegateで)「UserNotifications」をインポートすると、拡張する必要があります:

UNUserNotificationCenterDelegate

あなたはどこaskPermissionを呼び出すことができる、ということで行われたら

これで、通知をトリガーする権限があることを期待しています。あなたの答えのために、私はのために使用する必要がありますどのようなコード(Xcodeのに関連付けられていない実際のデバイス上の)通常の状況で通知を呼び出すという点で

func notification() { 

    let content = UNMutableNotificationContent() 
    content.body = "Body Of The Notification" 
    content.categoryIdentifier = "exampleCategoryIdentifier" // Re-Use the same identifier of the previous category. 
    content.sound = UNNotificationSound.default() // This is optional 

    let request = UNNotificationRequest(identifier: NSUUID().uuidString, 
             content: content, 
             trigger: nil) 
    let center = UNUserNotificationCenter.current() 

    center.add(request) { (error) in 
     if error != nil { 
      print(error!) 
     } else { 
      print("notification: ok") 
     } 
    } 
} 
+0

ありがとうございました!私の未解決の質問でも "一見"(笑)を取ることを検討してください。あなたが私を助けてくれるなら、 –

関連する問題