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

質問編集履歴

2

該当のソースコード、発生している問題を更新

2017/09/22 07:20

投稿

alan-d-haller
alan-d-haller

スコア18

title CHANGED
File without changes
body CHANGED
@@ -29,41 +29,154 @@
29
29
  どうぞ宜しくお願いいたします。
30
30
 
31
31
 
32
- ###発生している問題・エラーメッセージ
33
- 実機テストでFirebaseからのリモートPush通知を受け取れない。
34
-
35
-
36
- ###該当のソースコード
32
+ ###【更新】該当のソースコード
37
33
  ```Swift
38
34
  import UIKit
39
- import Firebase
40
35
  import UserNotifications
41
36
 
37
+ import Firebase
38
+
42
39
  @UIApplicationMain
43
40
  class AppDelegate: UIResponder, UIApplicationDelegate {
44
-
41
+
45
42
  var window: UIWindow?
46
-
43
+ let gcmMessageIDKey = "gcm.message_id"
44
+
47
-
45
+ func application(_ application: UIApplication,
48
- func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
46
+ didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
49
47
 
50
- let center = UNUserNotificationCenter.current()
48
+ FirebaseApp.configure()
49
+
51
- //get the notification center
50
+ // [START set_messaging_delegate]
51
+ Messaging.messaging().delegate = self
52
+ // [END set_messaging_delegate]
52
- center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
53
+ // Register for remote notifications. This shows a permission dialog on first run, to
54
+ // show the dialog at a more appropriate time move this registration accordingly.
55
+ // [START register_for_notifications]
56
+ if #available(iOS 10.0, *) {
53
- // Enable or disable features based on authorization.
57
+ // For iOS 10 display notification (sent via APNS)
58
+ UNUserNotificationCenter.current().delegate = self
54
59
 
60
+ let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
61
+ UNUserNotificationCenter.current().requestAuthorization(
62
+ options: authOptions,
63
+ completionHandler: {_, _ in })
64
+ } else {
65
+ let settings: UIUserNotificationSettings =
66
+ UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
67
+ application.registerUserNotificationSettings(settings)
55
68
  }
56
69
 
57
- FirebaseApp.configure()
70
+ application.registerForRemoteNotifications()
58
71
 
72
+ // [END register_for_notifications]
59
73
  return true
60
74
  }
61
75
 
76
+ // [START receive_message]
62
- func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
77
+ func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
78
+ // If you are receiving a notification message while your app is in the background,
79
+ // this callback will not be fired till the user taps on the notification launching the application.
80
+ // TODO: Handle data of notification
81
+ // With swizzling disabled you must let Messaging know about the message, for Analytics
82
+ // Messaging.messaging().appDidReceiveMessage(userInfo)
83
+ // Print message ID.
84
+ if let messageID = userInfo[gcmMessageIDKey] {
85
+ print("Message ID: \(messageID)")
86
+ }
63
87
 
88
+ // Print full message.
89
+ print(userInfo)
90
+ }
91
+
92
+ func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
93
+ fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
94
+ // If you are receiving a notification message while your app is in the background,
64
- InstanceID.instanceID().setAPNSToken(deviceToken as Data, type: InstanceIDAPNSTokenType.unknown)
95
+ // this callback will not be fired till the user taps on the notification launching the application.
96
+ // TODO: Handle data of notification
97
+ // With swizzling disabled you must let Messaging know about the message, for Analytics
98
+ // Messaging.messaging().appDidReceiveMessage(userInfo)
99
+ // Print message ID.
100
+ if let messageID = userInfo[gcmMessageIDKey] {
101
+ print("Message ID: \(messageID)")
102
+ }
65
103
 
104
+ // Print full message.
105
+ print(userInfo)
106
+
107
+ completionHandler(UIBackgroundFetchResult.newData)
66
108
  }
109
+ // [END receive_message]
110
+ func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
111
+ print("Unable to register for remote notifications: \(error.localizedDescription)")
112
+ }
113
+
114
+ // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
115
+ // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
116
+ // the FCM registration token.
117
+ func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
118
+ print("APNs token retrieved: \(deviceToken)")
119
+
120
+ // With swizzling disabled you must set the APNs token here.
121
+ // Messaging.messaging().apnsToken = deviceToken
122
+ }
123
+ }
124
+
125
+ // [START ios_10_message_handling]
126
+ @available(iOS 10, *)
127
+ extension AppDelegate : UNUserNotificationCenterDelegate {
128
+
129
+ // Receive displayed notifications for iOS 10 devices.
130
+ func userNotificationCenter(_ center: UNUserNotificationCenter,
131
+ willPresent notification: UNNotification,
132
+ withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
133
+ let userInfo = notification.request.content.userInfo
134
+
135
+ // With swizzling disabled you must let Messaging know about the message, for Analytics
136
+ // Messaging.messaging().appDidReceiveMessage(userInfo)
137
+ // Print message ID.
138
+ if let messageID = userInfo[gcmMessageIDKey] {
139
+ print("Message ID: \(messageID)")
140
+ }
141
+
142
+ // Print full message.
143
+ print(userInfo)
144
+
145
+ // Change this to your preferred presentation option
146
+ completionHandler([])
147
+ }
148
+
149
+ func userNotificationCenter(_ center: UNUserNotificationCenter,
150
+ didReceive response: UNNotificationResponse,
151
+ withCompletionHandler completionHandler: @escaping () -> Void) {
152
+ let userInfo = response.notification.request.content.userInfo
153
+ // Print message ID.
154
+ if let messageID = userInfo[gcmMessageIDKey] {
155
+ print("Message ID: \(messageID)")
156
+ }
157
+
158
+ // Print full message.
159
+ print(userInfo)
160
+
161
+ completionHandler()
162
+ }
163
+ }
164
+ // [END ios_10_message_handling]
165
+
166
+ extension AppDelegate : MessagingDelegate {
167
+ // [START refresh_token]
168
+ func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
169
+ print("Firebase registration token: \(fcmToken)")
170
+ }
171
+ // [END refresh_token]
172
+ // [START ios_10_data_message]
173
+ // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
174
+ // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
175
+ func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
176
+ print("Received data message: \(remoteMessage.appData)")
177
+ }
178
+ // [END ios_10_data_message]
179
+ }
67
180
  ```
68
181
 
69
182
  ###【更新】試したこと
@@ -76,6 +189,11 @@
76
189
 
77
190
  しかし、いずれもうまくいかず、以下のようなエラーがコンソールに出る。
78
191
 
192
+ ###【更新】発生している問題・エラーメッセージ
193
+ 実機テストでFirebaseからのリモートPush通知を受け取れない。
194
+
195
+ ![イメージ説明](5d51dc564dcc463f022c6fc1482f322c.png)
196
+
79
197
  ```Swift
80
198
  2017-09-22 15:55:59.494542+0900 Swift4RemotePushTest1[4966:1766557] [Firebase/Core][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'.
81
199
  2017-09-22 15:55:59.494 Swift4RemotePushTest1[4966] <Error> [Firebase/Core][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'.

1

試したことの内容を更新しました。

2017/09/22 07:20

投稿

alan-d-haller
alan-d-haller

スコア18

title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,7 @@
8
8
  [https://qiita.com/jiiikki/items/31f294cf2afcfe8d868d](https://qiita.com/jiiikki/items/31f294cf2afcfe8d868d)
9
9
 
10
10
  Podのインストールと、FirebaseへのAPNs証明書のアップロード、
11
- プロビジョニングファイルのインストールまでは問題なく完了できたのですが、
11
+ プロビジョニングプロファイルのインストールまでは問題なく完了できたのですが、
12
12
  最後のXcodeでプロジェクトの編集をする段階でつまづいています。
13
13
 
14
14
  上記参考ページのコードではビルドできなかったため、
@@ -66,10 +66,29 @@
66
66
  }
67
67
  ```
68
68
 
69
- ###試したこと
69
+ ###【更新】試したこと
70
- ・Qiitaの参考ページに記載のコードではビルドできなかったため、ご指導いただいている方から教えていただいた上記コードに変更した。
71
70
 
71
+ ・Firebase公式ガイドに記載されている手順の通りにコードを記述した。
72
+ [https://firebase.google.com/docs/cloud-messaging/ios/client?hl=ja](https://firebase.google.com/docs/cloud-messaging/ios/client?hl=ja)
72
73
 
74
+ ・上記公式ガイドにリンク付けされているGitHubのページ記載のクイックスタートをAppDelegateに丸ごとコピペしてみた。
75
+ [https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55](https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55)
76
+
77
+ しかし、いずれもうまくいかず、以下のようなエラーがコンソールに出る。
78
+
79
+ ```Swift
80
+ 2017-09-22 15:55:59.494542+0900 Swift4RemotePushTest1[4966:1766557] [Firebase/Core][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'.
81
+ 2017-09-22 15:55:59.494 Swift4RemotePushTest1[4966] <Error> [Firebase/Core][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'.
82
+ 2017-09-22 15:55:59.495386+0900 Swift4RemotePushTest1[4966:1766557] [Firebase/Core][I-COR000005] No app has been configured yet.
83
+ 2017-09-22 15:55:59.495 Swift4RemotePushTest1[4966] <Error> [Firebase/Core][I-COR000005] No app has been configured yet.
84
+ 2017-09-22 15:55:59.502100+0900 Swift4RemotePushTest1[4966:1766494] *** Terminating app due to uncaught exception 'com.firebase.core', reason: '`[FIRApp configure];` (`FirebaseApp.configure()` in Swift) could not find a valid GoogleService-Info.plist in your project. Please download one from https://console.firebase.google.com/.'
85
+ *** First throw call stack:
86
+ (0x183f1bd38 0x183430528 0x183f1bc80 0x1025b5830 0x102550c64 0x1025516d0 0x18d39a050 0x18d58d898 0x18d5926e4 0x18d820454 0x18daf01f0 0x18d8200b8 0x18d820928 0x18df896e8 0x18df8958c 0x18dd059c0 0x18de9afc8 0x18dd05870 0x18daef850 0x18d590e28 0x18d9946ec 0x1865bd768 0x1865c6070 0x1036f145c 0x1036fdb74 0x1865f1a04 0x1865f16a8 0x1865f1c44 0x183ec4358 0x183ec42d8 0x183ec3b60 0x183ec1738 0x183de22d8 0x185c73f84 0x18d38e880 0x1025542cc 0x18390656c)
87
+ libc++abi.dylib: terminating with uncaught exception of type NSException
88
+ (lldb)
89
+ ```
90
+
91
+
73
92
  ###補足情報(言語/FW/ツール等のバージョンなど)
74
93
  開発環境は以下の通りです。
75
94
  macOS Sierra(10.12.6)