【发布时间】:2019-10-15 19:04:09
【问题描述】:
我已经实现了FirebaseCloudMessaging,当应用程序位于Background 时得到notifications,但是当我安装新应用程序willPresent 和didReceive 通知delegate 30 到35 分钟后不会被调用开始打电话。
只有当我通过删除旧的应用程序来安装应用程序时才会发生这种情况。
这是我的代码,你可以检查我做错的地方
import UIKit
import Firebase
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Register for push notification
self.registerForNotification()
FirebaseApp.configure()
Messaging.messaging().delegate = self
return true
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
//MARK: - Register For RemoteNotification
func registerForNotification() {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { granted, error in }
UIApplication.shared.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
#if DEVELOPMENT
Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
#else
Messaging.messaging().setAPNSToken(deviceToken, type: .prod)
#endif
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
debugPrint("Unable to register for remote notifications: \(error.localizedDescription)")
}
//MARK: - UNUserNotificationCenterDelegate
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
debugPrint(userInfo)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
debugPrint(userInfo)
completionHandler([.badge, .alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print(userInfo)
completionHandler()
}
}
extension AppDelegate: MessagingDelegate {
//MARK: - MessagingDelegate
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print(fcmToken)
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}
}
感谢您的帮助
【问题讨论】:
-
安装应用程序后 30 分钟?还是启动应用程序后 30 分钟?我想您知道应用必须注册推送通知才能创建新令牌。
-
安装应用程序后 30 分钟。当应用程序处于后台时,我会收到通知。注册过程正常。
-
我的意思是安装应用程序是不够的。你必须启动它来调用 registerForNotification()。当权限请求警报出现时,用户必须接受。
-
即使您从 Firebase 控制台发送手动通知也会发生这种情况吗?
-
@Gabriel,当应用程序处于后台通知时,收到意味着我调用了 registerForNotification() 函数并且还允许了权限。谢谢。
标签: ios swift push-notification firebase-cloud-messaging usernotifications