回答編集履歴

4

修正

2016/10/22 03:03

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -6,12 +6,8 @@
6
6
 
7
7
 
8
8
 
9
- 参考にして作ってみました。
10
-
11
9
  通知を受けた時にロック画面では以下の様な見え方になりました。
12
10
 
13
-
14
-
15
11
  ![s](6d9c830b5cbc28aed27a34a81f5bbc88.png)
16
12
 
17
13
 

3

修正

2016/10/22 03:02

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -1,4 +1,4 @@
1
- 試してませんが、以下のサイトを参考にしてみるとできるのでは無でしょうか?
1
+ 以下のサイトを参考にしてみてくださ
2
2
 
3
3
 
4
4
 
@@ -13,3 +13,245 @@
13
13
 
14
14
 
15
15
  ![s](6d9c830b5cbc28aed27a34a81f5bbc88.png)
16
+
17
+
18
+
19
+ 確認したコード
20
+
21
+
22
+
23
+ ★ AppDelegate.swift
24
+
25
+
26
+
27
+ ```swift
28
+
29
+ import UIKit
30
+
31
+ import UserNotifications
32
+
33
+
34
+
35
+ @UIApplicationMain
36
+
37
+ class AppDelegate: UIResponder, UIApplicationDelegate {
38
+
39
+
40
+
41
+ var window: UIWindow?
42
+
43
+
44
+
45
+
46
+
47
+ func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
48
+
49
+
50
+
51
+
52
+
53
+ if #available(iOS 10.0, *) {
54
+
55
+ // iOS 10
56
+
57
+ let center = UNUserNotificationCenter.current()
58
+
59
+ center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { (granted, error) in
60
+
61
+ if error != nil {
62
+
63
+ return
64
+
65
+ }
66
+
67
+
68
+
69
+ if granted {
70
+
71
+ debugPrint("通知許可")
72
+
73
+ } else {
74
+
75
+ debugPrint("通知拒否")
76
+
77
+ }
78
+
79
+ })
80
+
81
+
82
+
83
+ } else {
84
+
85
+ // iOS 9
86
+
87
+ let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
88
+
89
+ UIApplication.shared.registerUserNotificationSettings(settings)
90
+
91
+ }
92
+
93
+ return true
94
+
95
+ }
96
+
97
+ }
98
+
99
+
100
+
101
+ ```
102
+
103
+
104
+
105
+ ★ ViewController.swift
106
+
107
+
108
+
109
+ ```
110
+
111
+ import UIKit
112
+
113
+ import UserNotifications
114
+
115
+
116
+
117
+ enum ActionIdentifier: String {
118
+
119
+ case attend
120
+
121
+ case absent
122
+
123
+ }
124
+
125
+
126
+
127
+ class ViewController: UIViewController, UNUserNotificationCenterDelegate {
128
+
129
+
130
+
131
+ override func viewDidLoad() {
132
+
133
+ super.viewDidLoad()
134
+
135
+
136
+
137
+ }
138
+
139
+
140
+
141
+ @IBOutlet weak var a: UIButton!
142
+
143
+
144
+
145
+ @IBAction func g(_ sender: AnyObject) {
146
+
147
+
148
+
149
+ let attend = UNNotificationAction(identifier: ActionIdentifier.attend.rawValue,
150
+
151
+ title: "出席", options: [])
152
+
153
+
154
+
155
+ let absent = UNNotificationAction(identifier: ActionIdentifier.absent.rawValue,
156
+
157
+ title: "欠席",
158
+
159
+ options: [])
160
+
161
+
162
+
163
+
164
+
165
+ let category = UNNotificationCategory(identifier: "message", actions: [attend, absent], intentIdentifiers: [], options: [])
166
+
167
+
168
+
169
+ UNUserNotificationCenter.current().setNotificationCategories([category])
170
+
171
+ UNUserNotificationCenter.current().delegate = self
172
+
173
+
174
+
175
+
176
+
177
+ let content = UNMutableNotificationContent()
178
+
179
+ content.title = "出席確認"
180
+
181
+ content.body = "今日のイベントに参加しますか?"
182
+
183
+ content.sound = UNNotificationSound.default()
184
+
185
+
186
+
187
+ // categoryIdentifierを設定
188
+
189
+ content.categoryIdentifier = "message"
190
+
191
+
192
+
193
+ // 5秒後
194
+
195
+ let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
196
+
197
+ let request = UNNotificationRequest(identifier: "FiveSecond",
198
+
199
+ content: content,
200
+
201
+ trigger: trigger)
202
+
203
+
204
+
205
+ UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
206
+
207
+ }
208
+
209
+
210
+
211
+
212
+
213
+ @available(iOS 10.0, *)
214
+
215
+ func userNotificationCenter(_ center: UNUserNotificationCenter,
216
+
217
+ didReceive response: UNNotificationResponse,
218
+
219
+ withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
220
+
221
+
222
+
223
+ switch response.actionIdentifier {
224
+
225
+ case ActionIdentifier.attend.rawValue:
226
+
227
+ debugPrint("出席します")
228
+
229
+ case ActionIdentifier.absent.rawValue:
230
+
231
+ debugPrint("欠席します")
232
+
233
+ default:
234
+
235
+ ()
236
+
237
+ }
238
+
239
+
240
+
241
+ completionHandler()
242
+
243
+ }
244
+
245
+
246
+
247
+ override func didReceiveMemoryWarning() {
248
+
249
+ super.didReceiveMemoryWarning()
250
+
251
+
252
+
253
+ }
254
+
255
+ }
256
+
257
+ ```

2

修正

2016/10/22 03:02

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -6,7 +6,9 @@
6
6
 
7
7
 
8
8
 
9
+ 参考にして作ってみました。
10
+
9
- 参考にして通知を受けた時にロック画面では以下の様な見え方になりました。
11
+ 通知を受けた時にロック画面では以下の様な見え方になりました。
10
12
 
11
13
 
12
14
 

1

修正

2016/10/21 23:56

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -3,3 +3,11 @@
3
3
 
4
4
 
5
5
  [<Swift>iOS 10 User Notifications Framework実装まとめ](http://qiita.com/mshrwtnb/items/3135e931eedc97479bb5)
6
+
7
+
8
+
9
+ 参考にして通知を受けた時にロック画面では以下の様な見え方になりました。
10
+
11
+
12
+
13
+ ![s](6d9c830b5cbc28aed27a34a81f5bbc88.png)