回答編集履歴

2

修正

2016/07/10 06:46

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -9,3 +9,95 @@
9
9
 
10
10
 
11
11
  ![i](63f2c7074e788d9f2bb28a635f8e8161.png)
12
+
13
+
14
+
15
+
16
+
17
+ アプリが起動中に通知を受けた場合の、アラートの表示コードを載せておきます。
18
+
19
+
20
+
21
+ ```swift
22
+
23
+ import UIKit
24
+
25
+
26
+
27
+ @UIApplicationMain
28
+
29
+ class AppDelegate: UIResponder, UIApplicationDelegate {
30
+
31
+
32
+
33
+ var window: UIWindow?
34
+
35
+
36
+
37
+ func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
38
+
39
+
40
+
41
+ let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories:nil)
42
+
43
+ UIApplication.sharedApplication().registerUserNotificationSettings(settings)
44
+
45
+ UIApplication.sharedApplication().registerForRemoteNotifications()
46
+
47
+
48
+
49
+ return true
50
+
51
+ }
52
+
53
+
54
+
55
+ func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
56
+
57
+
58
+
59
+ if application.applicationState == .Active {
60
+
61
+
62
+
63
+ if let title = notification.alertTitle, let body = notification.alertBody {
64
+
65
+ let alert = UIAlertController(title: title, message: body, preferredStyle: .Alert)
66
+
67
+ alert.addAction( UIAlertAction(title: "OK", style: .Default, handler: nil))
68
+
69
+ UIApplication.sharedApplication().topViewController()?.presentViewController(alert, animated: true, completion: nil)
70
+
71
+ }
72
+
73
+ }
74
+
75
+ }
76
+
77
+ }
78
+
79
+
80
+
81
+
82
+
83
+ extension UIApplication {
84
+
85
+ func topViewController() -> UIViewController? {
86
+
87
+ guard var topViewController = UIApplication.sharedApplication().keyWindow?.rootViewController else { return nil }
88
+
89
+
90
+
91
+ while let presentedViewController = topViewController.presentedViewController {
92
+
93
+ topViewController = presentedViewController
94
+
95
+ }
96
+
97
+ return topViewController
98
+
99
+ }
100
+
101
+ }
102
+
103
+ ```

1

修正

2016/07/10 06:46

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- アプリを起動中に通知を受けた場合は所定のハンドラで処理を書き画面にアラートを表示することは可能です。
7
+ アプリを起動中に通知を受けた場合、バナー表示されませんが所定のハンドラで処理を書き画面にアラートを表示することは可能です。
8
8
 
9
9
 
10
10