【发布时间】:2019-09-14 12:39:44
【问题描述】:
推送通知根本不起作用。到目前为止,我已经尝试了所有可能的措施:
这是我尝试过的代码:
在didFinishLaunchingWithOptions
FirebaseApp.configure()
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.badge, .alert, .sound]) {
(granted, error) in
if granted {
DispatchQueue.main.async {
application.registerForRemoteNotifications()
//UIApplication.shared.registerForRemoteNotifications()
}
} else {
//print("APNS Registration failed")
//print("Error: \(String(describing: error?.localizedDescription))")
}
}
} else {
let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
let setting = UIUserNotificationSettings(types: type, categories: nil)
application.registerUserNotificationSettings(setting)
application.registerForRemoteNotifications()
//UIApplication.shared.registerForRemoteNotifications()
}
然后是注册和失败方法:
private func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
print("Registered Notification")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error.localizedDescription)
print("Not registered notification")
}
@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
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo["gcm.message_id"] {
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["gcm.message_id"] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler()
}
}
注意:
- 我已经在真机上试过了,目前没有推送通知。
- 我在打开推送通知后仔细检查了证书并重新生成了配置文件 能力。
- 我还添加了后台模式 -> 远程通知开启。
- 我尝试过使用旧版构建也没有成功。
- 我尝试重新安装应用程序很多次都不起作用。
- FirebaseAppDelegateProxyEnabled 在 plist 中设置为 NO 仍然没有运气。
- 还更新了 pod,但仍然没有运气。
- .p12 证书位于 firebase 控制台,但仍无法正常工作。
从过去 1 周开始尝试使用 Apple 密钥使用不同身份验证方法的不同项目,我也尝试过仍然没有运气。
【问题讨论】:
-
您是否检查过在单个设备令牌上发送 Firebase 测试通知?
-
@Nikung 我正在尝试使用 firebase 发送消息,但暂时不支持后端,无法正常工作,是的,我正在尝试使用多个令牌而不是单个令牌。
-
在这种情况下你会得到什么输出:如果授予 { DispatchQueue.main.async { application.registerForRemoteNotifications() //UIApplication.shared.registerForRemoteNotifications() } } else { print("APNS 注册失败") print("错误:(String(描述:错误?.localizedDescription))") }
-
@Nikunj 堆栈上有人说 application.registerForRemoteNotifications() 应该在主线程上而不是在后台线程上,输出是它正在被授予并且成功代码正在运行。
标签: ios swift firebase firebase-cloud-messaging apple-push-notifications