0

「クイック返信」や「キャンセル」のような2つのアクションボタンを使用して通知を作成しようとしていますが、これに関するコード例は見つかりません。誰かがSwift 3でこれを行う方法を説明することができます。Swiftローカルプッシュ通知アクションビュー

+0

iOS 10を使用しようとしていますか? –

+0

はい@SalmanGhumsani –

答えて

0

あなたが探しているものの正確な例を与えることは難しいです。アクションのある即時実行UNUserNotificationCenterの実装です。

import UserNotifications 

セットアップ

private let categoryID = "Category" 

IDが一定のカテゴリを定義し、通知

をトリガするため UNUserNotificationCenter

// MARK: - Lifecycle 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Configure User Notification Center 
    UNUserNotificationCenter.current().delegate = self 
    // Define Actions 
    let actionShowSomething = UNNotificationAction(identifier: "ShowSomething", title: "Show Something", options: []) 

    // Define Category 
    let category = UNNotificationCategory(identifier: categoryID, actions: [actionShowSomething], intentIdentifiers: [], options: []) 

    // Register Category 
    UNUserNotificationCenter.current().setNotificationCategories([category]) 
} 

例のイベントを登録

// MARK: - Actions @IBAction func sheduleNotification(sender: UIButton) { // Request Notification Settings UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in switch notificationSettings.authorizationStatus { case .notDetermined: self.requestAuthorization(completionHandler: { (success) in guard success else { return } // Schedule Local Notification self.scheduleLocalNotification() }) case .authorized: // Schedule Local Notification self.scheduleLocalNotification() case .denied: print("Application Not Allowed to Display Notifications") } } } 

sheduleNotification実装

// MARK: - Methods 

private func scheduleLocalNotification() { 
    // Create Notification Content 
    let notificationContent = UNMutableNotificationContent() 

    // Configure Notification Content 
    notificationContent.title = "Title" 
    notificationContent.subtitle = "Subtitle" 
    notificationContent.body = "Body" 

    // Set Category Identifier 
    notificationContent.categoryIdentifier = categoryID 

    // Add Trigger 
    let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false) 

    // Create Notification Request 
    let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger) 

    // Add Request to User Notification Center 
    UNUserNotificationCenter.current().add(notificationRequest) { (error) in 
     if let error = error { 
      print("Unable to Add Notification Request (\(error), \(error.localizedDescription))") 
     } 
    } 
} 

ハンドルユーザ通知許可

private func requestAuthorization(completionHandler: @escaping (_ success: Bool) ->()) { 
    // Request Authorization 
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (success, error) in 
     if let error = error { 
      print("Request Authorization Failed (\(error), \(error.localizedDescription))") 
     } 
     completionHandler(success) 
    } 
} 

UnUserNotificationDelegate実装

詳細については、こちらを見てのため

結果

Push example at runtime

関連する問題