Swift 3 Firebase 远程通知未在 TestFlight 上显示
Posted
技术标签:
【中文标题】Swift 3 Firebase 远程通知未在 TestFlight 上显示【英文标题】:Swift 3 Firebase Remote Notification not showing on TestFlight 【发布时间】:2017-04-19 06:00:55 【问题描述】:美好的一天,我这里有这个应用程序,它可以完美地显示插入 Xcode 的设备上的通知,但当我尝试在 TestFlight 或已上传到 App Store 的实际应用程序中运行它时,它无法正常工作。我试过撤销和制作新证书,但没有。帮助将不胜感激。这是我的 AppDelegate:
import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import CoreData
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
// [START register_for_notifications]
if #available(ios 10.0, *)
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: _, _ in )
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
else
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
// [END register_for_notifications]
FIRApp.configure()
// [START add_token_refresh_observer]
// Add observer for InstanceID token refresh callback.
NotificationCenter.default.addObserver(self,
selector: #selector(self.tokenRefreshNotification),
name: .firInstanceIDTokenRefresh,
object: nil)
// [END add_token_refresh_observer]
return true
// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey]
print("Message ID: \(messageID)")
// Print full message.
print(userInfo)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey]
print("Message ID: \(messageID)")
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
// [END receive_message]
// [START refresh_token]
func tokenRefreshNotification(_ notification: Notification)
if let refreshedToken = FIRInstanceID.instanceID().token()
print("InstanceID token: \(refreshedToken)")
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
// [END refresh_token]
// [START connect_to_fcm]
func connectToFcm()
// Won't connect since there is no token
guard FIRInstanceID.instanceID().token() != nil else
return
// Disconnect previous FCM connection if it exists.
FIRMessaging.messaging().disconnect()
FIRMessaging.messaging().connect (error) in
if error != nil
print("Unable to connect with FCM. \(error?.localizedDescription ?? "")")
else
print("Connected to FCM.")
// [END connect_to_fcm]
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
print("Unable to register for remote notifications: \(error.localizedDescription)")
// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
// the InstanceID token.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
print("APNs token retrieved: \(deviceToken)")
// With swizzling disabled you must set the APNs token here.
// FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)
// [START connect_on_active]
func applicationDidBecomeActive(_ application: UIApplication)
connectToFcm()
// [END connect_on_active]
// [START disconnect_from_fcm]
func applicationDidEnterBackground(_ application: UIApplication)
FIRMessaging.messaging().disconnect()
print("Disconnected from FCM.")
// [END disconnect_from_fcm]
// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
let userInfo = notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey]
print("Message ID: \(messageID)")
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([])
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey]
print("Message ID: \(messageID)")
// Print full message.
print(userInfo)
completionHandler()
// [END ios_10_message_handling]
// [START ios_10_data_message_handling]
extension AppDelegate : FIRMessagingDelegate
// Receive data message on iOS 10 devices while app is in the foreground.
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage)
print(remoteMessage.appData)
// [END ios_10_data_message_handling]
【问题讨论】:
只关心接收消息吗?你能正确收到注册令牌吗? 收到注册令牌是什么意思? @AL。当它像 Xcode 上的测试设备一样连接时,它会显示通知,但不会在 TestFlight 上显示。不确定我是否必须弄乱设备 ID 令牌。 你是如何发送消息的?通过应用服务器或 Firebase 通知控制台?如果您可以确认您能够generate a registration token in your app,这将有助于缩小问题所在。 【参考方案1】:确保您已在 Firebase 控制台中上传了生产 APNs 证书。我认为您只上传了开发 APNs 证书,因此您可以使用 Xcode 获取通知。此外,出于测试目的使用 firebase 通知控制台发送通知,您只需在控制台中提供设备令牌。
【讨论】:
【参考方案2】:将此代码添加到 AppDelegate 文件中
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.prod)
您需要为sandbox
和prod
设置APNS 令牌。
【讨论】:
以上是关于Swift 3 Firebase 远程通知未在 TestFlight 上显示的主要内容,如果未能解决你的问题,请参考以下文章
在控制台中收到 Firebase 云消息通知但未在手机中显示 - Swift App
在 iOS (swift) 应用中,第二个 Firebase 应用的注册无法接收远程通知