質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

4966閲覧

(swift3) アクション付きローカル通知の実装について

bbdd

総合スコア43

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

1クリップ

投稿2016/10/21 19:32

編集2016/10/22 08:12

###前提・実現したいこと
ローカル通知にボタンを配置したい。

この図のようなものをつくりたい。
(参照 http://dev.classmethod.jp/references/ios8-notification-action/)

ロック画面でスライドした時に現れるボタンの編集がしたい。

###発生している問題・エラーメッセージ

現状ですと上記のようにロック画面でのアクションがclearのみになっている。
ここに現れるボタンの編集がしたい。

(参照サイト http://qiita.com/mshrwtnb/items/3135e931eedc97479bb5)


このようなアクション通知は実装できています。

言語:swift
User Notifications Frameworkを使って実装したいが、やり方がわからない。

###試したこと
AppDelegate.swift

swift

1func applicationDidEnterBackground(_ application: UIApplication) { 2 3 enum ActionIdentifier: String { 4 case attend 5 case absent 6 } 7 8 let attend = UNNotificationAction(identifier: ActionIdentifier.attend.rawValue, 9 title: "出席", options: []) 10 11 let absent = UNNotificationAction(identifier: ActionIdentifier.absent.rawValue, 12 title: "欠席", 13 options: []) 14 15 16 let category = UNNotificationCategory(identifier: "message", actions: [attend, absent], intentIdentifiers: [], options: []) 17 18 19 UNUserNotificationCenter.current().setNotificationCategories([category]) 20 21 22 23 let content = UNMutableNotificationContent() 24 content.title = "出席確認" 25 content.body = "今日のイベントに参加しますか?" 26 content.sound = UNNotificationSound.default() 27 28 // categoryIdentifierを設定 29 content.categoryIdentifier = "message" 30 31 // 5秒後 32 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) 33 let request = UNNotificationRequest(identifier: "FiveSecond", 34 content: content, 35 trigger: trigger) 36 37 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 38 39 @available(iOS 10.0, *) 40 func userNotificationCenter(_ center: UNUserNotificationCenter, 41 didReceive response: UNNotificationResponse, 42 withCompletionHandler completionHandler: () -> Void) { 43 44 switch response.actionIdentifier { 45 case ActionIdentifier.attend.rawValue: 46 debugPrint("出席します") 47 case ActionIdentifier.absent.rawValue: 48 debugPrint("欠席します") 49 default: 50 () 51 } 52 53 completionHandler() 54 } 55 56 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下のサイトを参考にしてみてください。

<Swift>iOS 10 User Notifications Framework実装まとめ

通知を受けた時にロック画面では以下の様な見え方になりました。
s

確認したコード

★ AppDelegate.swift

swift

1import UIKit 2import UserNotifications 3 4@UIApplicationMain 5class AppDelegate: UIResponder, UIApplicationDelegate { 6 7 var window: UIWindow? 8 9 10 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 11 12 13 if #available(iOS 10.0, *) { 14 // iOS 10 15 let center = UNUserNotificationCenter.current() 16 center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { (granted, error) in 17 if error != nil { 18 return 19 } 20 21 if granted { 22 debugPrint("通知許可") 23 } else { 24 debugPrint("通知拒否") 25 } 26 }) 27 28 } else { 29 // iOS 9 30 let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil) 31 UIApplication.shared.registerUserNotificationSettings(settings) 32 } 33 return true 34 } 35} 36

★ ViewController.swift

import UIKit import UserNotifications enum ActionIdentifier: String { case attend case absent } class ViewController: UIViewController, UNUserNotificationCenterDelegate { override func viewDidLoad() { super.viewDidLoad() } @IBOutlet weak var a: UIButton! @IBAction func g(_ sender: AnyObject) { let attend = UNNotificationAction(identifier: ActionIdentifier.attend.rawValue, title: "出席", options: []) let absent = UNNotificationAction(identifier: ActionIdentifier.absent.rawValue, title: "欠席", options: []) let category = UNNotificationCategory(identifier: "message", actions: [attend, absent], intentIdentifiers: [], options: []) UNUserNotificationCenter.current().setNotificationCategories([category]) UNUserNotificationCenter.current().delegate = self let content = UNMutableNotificationContent() content.title = "出席確認" content.body = "今日のイベントに参加しますか?" content.sound = UNNotificationSound.default() // categoryIdentifierを設定 content.categoryIdentifier = "message" // 5秒後 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) } @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) { switch response.actionIdentifier { case ActionIdentifier.attend.rawValue: debugPrint("出席します") case ActionIdentifier.absent.rawValue: debugPrint("欠席します") default: () } completionHandler() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }

投稿2016/10/21 23:42

編集2016/10/22 03:03
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bbdd

2016/10/22 08:20

回答ありがとうございます。早く回答を頂き嬉しく思います。 回答いただいたようなアクション通知は実装できました。 しかし、ロック画面におけるスライド操作によって表れるボタンは編集されていません。 実装方法をお知りであればご教授願いたいです。
_Kentarou

2016/10/22 08:41 編集

iOS8の Notification Action が iOS10の UNNotificationActionに置き換わっていると思うので同じ事ができていると思いますよ。 iOS8ではボタンが見えてそのまま更に横にスワイプすると削除されるのではなかったでしたっけ?なので見え方が変わっただけと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問