teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

修正

2016/07/10 06:46

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -3,4 +3,50 @@
3
3
 
4
4
  アプリを起動中に通知を受けた場合、バナーは表示されませんが所定のハンドラで処理を書き画面にアラートを表示することは可能です。
5
5
 
6
- ![i](63f2c7074e788d9f2bb28a635f8e8161.png)
6
+ ![i](63f2c7074e788d9f2bb28a635f8e8161.png)
7
+
8
+
9
+ アプリが起動中に通知を受けた場合の、アラートの表示コードを載せておきます。
10
+
11
+ ```swift
12
+ import UIKit
13
+
14
+ @UIApplicationMain
15
+ class AppDelegate: UIResponder, UIApplicationDelegate {
16
+
17
+ var window: UIWindow?
18
+
19
+ func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
20
+
21
+ let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories:nil)
22
+ UIApplication.sharedApplication().registerUserNotificationSettings(settings)
23
+ UIApplication.sharedApplication().registerForRemoteNotifications()
24
+
25
+ return true
26
+ }
27
+
28
+ func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
29
+
30
+ if application.applicationState == .Active {
31
+
32
+ if let title = notification.alertTitle, let body = notification.alertBody {
33
+ let alert = UIAlertController(title: title, message: body, preferredStyle: .Alert)
34
+ alert.addAction( UIAlertAction(title: "OK", style: .Default, handler: nil))
35
+ UIApplication.sharedApplication().topViewController()?.presentViewController(alert, animated: true, completion: nil)
36
+ }
37
+ }
38
+ }
39
+ }
40
+
41
+
42
+ extension UIApplication {
43
+ func topViewController() -> UIViewController? {
44
+ guard var topViewController = UIApplication.sharedApplication().keyWindow?.rootViewController else { return nil }
45
+
46
+ while let presentedViewController = topViewController.presentedViewController {
47
+ topViewController = presentedViewController
48
+ }
49
+ return topViewController
50
+ }
51
+ }
52
+ ```

1

修正

2016/07/10 06:46

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,6 +1,6 @@
1
1
  通知を`アラート(ダイアログ)`の様に表示したいということだと思いますが、
2
2
  こちらはユーザーが任意で設定できる場所であるため、プログラムからの制御は無理だと思います。
3
3
 
4
- アプリを起動中に通知を受けた場合、所定のハンドラで処理を書き画面にアラートを表示することは可能です。
4
+ アプリを起動中に通知を受けた場合、バナーは表示されませんが所定のハンドラで処理を書き画面にアラートを表示することは可能です。
5
5
 
6
6
  ![i](63f2c7074e788d9f2bb28a635f8e8161.png)