0
一旦我再次打開應用程序,通知就會正常開始。以下是我的didFinish
方法。應用程序24小時待機後未收到Firebase通知
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// [CONFIGURE Firebase App]
FirebaseApp.configure()
// [ENABLE Firebase Persistance]
Database.database().isPersistenceEnabled = true
// Setting Api Key for Google maps
GMSServices.provideAPIKey("AIzaSyDSBDWuW4U3655ifuS6H0LJ9conLrX9_5Q")
let check = UserDefaults.standard.value(forKey: "CHECK_LOGIN") as? String
if check == "YES"
{
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "ROOTVIEW") as! DLDemoRootViewController
self.window?.rootViewController = nextViewController
}else
{
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "loginVIew") as! LoginVC
self.window?.rootViewController = nextViewController
}
// 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 })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
// Register for Remote Notification
application.registerForRemoteNotifications()
// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]
return true
}
// [START ios_10_data_message_handling]
extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase Refreshed registration token: \(fcmToken)")
UserDefaults.standard.set(fcmToken, forKey: "token")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "tokenRefresh"), object: nil)
}
// [END ios_10_data_message]
// [START ios_10_data_message]
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
// To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
// [END ios_10_data_message]
}
閱讀我對這個問題的回答https://stackoverflow.com/questions/37899712/fcm-background-notifications-not-working-在-IOS/37899773#37899773 – Chris