我用Firebase发送了一个通知,但是通知没有发到手机上。我想代码出了点问题。你能检查一下问题出在哪里吗?我正在通过Firebase发送云消息,但它不工作,没有通知。我的应用程序已安装在apple Store中。我正确加载了证书。
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
}
func application(_ application: UIApplication,
didReceiveRemoteNotification notification: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(notification) {
completionHandler(UIBackgroundFetchResult.noData)
return
}
// This notification is not auth related, developer should handle it.
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Pass device token to auth
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)
// Further handling of the device token if needed by the app
// ...
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
}
/// Register for push notifications
func registerForPushNotification(){
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
} else {
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
}
}
}
extension AppDelegate: MessagingDelegate {
func application(received remoteMessage: MessagingRemoteMessage) {
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print(fcmToken)
}
}
class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//UIApplication.shared.isIdleTimerDisabled = true
UIApplication.shared.setMinimumBackgroundFetchInterval(1800)
Messaging.messaging().delegate = self
FirebaseApp.configure()
Fabric.sharedSDK().debug = true
Fabric.with([Crashlytics.self()])
self.registerForPushNotification()
}
func application(_ application : UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
{
if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false {
return true
}
// other URL handling goes here.
return false
}发布于 2019-07-28 00:37:49
我会关注通过Firebase Cloud Messaging发送和接收推送通知的this tutorial。设备可能需要服务器密钥和令牌来发送/接收本教程向您展示如何提供的通知。请确保不要将服务器密钥直接硬编码到您的代码中。你应该把它放入一个全局常量文件中,然后从那里获取它。
https://stackoverflow.com/questions/57233032
复制相似问题