【发布时间】:2018-09-06 10:00:56
【问题描述】:
我正在尝试获取从 firebase 云信使发送到单个设备的通知。我已经安装了 firebase,当我运行程序时,控制台会打印出我尝试用来向该设备发送消息的 fcm 令牌(我使用的是真正的 iPhone)。消息发送正常,但我的 iPhone 上没有出现任何内容。我在 App Delegate 中实现了以下代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
if #available(iOS 10.0, *)
{
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) { (granted, error) in
if granted
{
//self.registerCategory()
}
}
// For iOS 10 data message (sent via FCM)
Messaging.messaging().delegate = self
}
else
{
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
//Configuring Firebase
FirebaseApp.configure()
// Add observer for InstanceID token refresh callback.
NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
return true
}
//Receive Remote Notification on Background
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
{
Messaging.messaging().appDidReceiveMessage(userInfo)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
Messaging.messaging().apnsToken = deviceToken
}
@objc func tokenRefreshNotification(notification: NSNotification)
{
if let refreshedToken = InstanceID.instanceID().token()
{
print("InstanceID token: \(refreshedToken)")
}
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
func connectToFcm()
{
Messaging.messaging().shouldEstablishDirectChannel = true
}
func applicationDidBecomeActive(application: UIApplication)
{
connectToFcm()
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
This is the response I get from the Firebase end
所以它说没有发送任何消息,我对此感到困惑,因为这是我的设备刚刚输出的令牌。
如何了解或解决此问题?
【问题讨论】:
-
您是否上传了用于发送通知的授权密钥?请点击以下链接:firebase.google.com/docs/cloud-messaging/ios/certs
-
1.仔细检查@vishalwaka 发布的页面中的信息(即捆绑 ID 等) 2. 如果您使用的是证书(而不是密钥),请确保您使用的是开发密钥而不是生产密钥
-
您是否在您的应用程序
info.plist文件中设置了属性FirebaseAppDelegateProxyEnabled。如果你做了,你给了它什么价值? -
@Barns 我在 Info.plist 中将 FirebaseAppDelegateProxyEnabled 设置为 NO
-
@PeterTao 我正在使用 APN 密钥或 .p8 文件
标签: ios swift firebase firebase-cloud-messaging