前提・実現したいこと
iOSアプリをxcodeで作っています。
FirebaseCloudMessagingを用いてプッシュ通知を送ろうとしています。
xcodeでこちらのページを参考にFCMを用いてプッシュ通知を実装しました。APNs証明書ではなく認証キーを使っています。実機デバックでは通知ができました。
ですが、いざストアにアップロードしてみると、アプリから通知の許可は求められるものの、通知が来ませんでした。
どのように実装すれば、通知が来るようになるのでしょうか??
AppDelegateのコードです
swift
1import UIKit 2import Firebase 3import FirebaseMessaging 4import UserNotifications 5 6@UIApplicationMain 7 8class AppDelegate: UIResponder, UIApplicationDelegate { 9 10 var window: UIWindow? 11 12 13 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 14 15 FirebaseApp.configure() 16 17 if #available(iOS 10.0, *) { 18 // For iOS 10 display notification (sent via APNS) 19 UNUserNotificationCenter.current().delegate = self 20 21 let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 22 UNUserNotificationCenter.current().requestAuthorization( 23 options: authOptions, 24 completionHandler: {_, _ in }) 25 } else { 26 let settings: UIUserNotificationSettings = 27 UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 28 application.registerUserNotificationSettings(settings) 29 } 30 31 application.registerForRemoteNotifications() 32 // Override point for customization after application launch. 33 return true 34 } 35 36 func applicationWillResignActive(_ application: UIApplication) { 37 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 38 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 39 } 40 41 func applicationDidEnterBackground(_ application: UIApplication) { 42 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 43 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 44 } 45 46 func applicationWillEnterForeground(_ application: UIApplication) { 47 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 48 } 49 50 func applicationDidBecomeActive(_ application: UIApplication) { 51 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 52 } 53 54 func applicationWillTerminate(_ application: UIApplication) { 55 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 56 } 57 58 59} 60 61func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 62 // Print message ID. 63 if let messageID = userInfo["gcm.message_id"] { 64 print("Message ID: (messageID)") 65 } 66 67 // Print full message. 68 print(userInfo) 69} 70 71 72func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 73 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 74 // Print message ID. 75 if let messageID = userInfo["gcm.message_id"] { 76 print("Message ID: (messageID)") 77 } 78 79 // Print full message. 80 print(userInfo) 81 82 completionHandler(UIBackgroundFetchResult.newData) 83} 84 85@available(iOS 10, *) 86extension AppDelegate : UNUserNotificationCenterDelegate { 87 func userNotificationCenter(_ center: UNUserNotificationCenter, 88 willPresent notification: UNNotification, 89 withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 90 let userInfo = notification.request.content.userInfo 91 92 if let messageID = userInfo["gcm.message_id"] { 93 print("Message ID: (messageID)") 94 } 95 96 print(userInfo) 97 98 completionHandler([]) 99 } 100 101 func userNotificationCenter(_ center: UNUserNotificationCenter, 102 didReceive response: UNNotificationResponse, 103 withCompletionHandler completionHandler: @escaping () -> Void) { 104 let userInfo = response.notification.request.content.userInfo 105 if let messageID = userInfo["gcm.message_id"] { 106 print("Message ID: (messageID)") 107 } 108 109 print(userInfo) 110 111 completionHandler() 112 } 113}
懸念点
上記の参考にしたページは、Appデベロッパーで端末登録をしているため、それに代替する作業が必要なのではと考えています。
調べているうちに、代替作業はTokenの実装なのではないかと思ったのですが、よくわかっていません。
よろしくお願いします。
補足情報
Xcode9.2
回答3件
あなたの回答
tips
プレビュー