Ich versuche, eine Standortbenachrichtigung zu senden, wenn ich eine FCM-Remotebenachrichtigung erhalte, da die FCM-Benachrichtigung, soweit ich weiß, keine Aktionsschaltflächen unterstützt. Aber die lokale Benachrichtigung wird nur manchmal gesendet und es scheint mir, dass es zufällig ist. Ich bekomme aber immer die Fernmeldung. Ich möchte die lokale Benachrichtigung in allen drei Zuständen abrufen, im Vordergrund, im Hintergrund oder beim Beenden der App.Warum wird meine lokale Benachrichtigung nicht gesendet, wenn ich eine Remote-Benachrichtigung erhalte?
Hier ist, wo ich die Tasten in didFinishLoadingWithOptions hinzufügen:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let incrementAction = UIMutableUserNotificationAction()
incrementAction.identifier = "First"
incrementAction.title = "First"
incrementAction.activationMode = UIUserNotificationActivationMode.foreground
incrementAction.isAuthenticationRequired = true
incrementAction.isDestructive = true
let decrementAction = UIMutableUserNotificationAction()
decrementAction.identifier = "second"
decrementAction.title = "second"
decrementAction.activationMode = UIUserNotificationActivationMode.foreground
decrementAction.isAuthenticationRequired = true
decrementAction.isDestructive = false
// Category
let counterCategory = UIMutableUserNotificationCategory()
counterCategory.identifier = "BOOKING_CATEGORY"
// A. Set actions for the default context
counterCategory.setActions([incrementAction, decrementAction],
for: UIUserNotificationActionContext.default)
// B. Set actions for the minimal context //No more than 2
counterCategory.setActions([incrementAction, decrementAction],
for: UIUserNotificationActionContext.minimal)
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: NSSet(object: counterCategory) as? Set<UIUserNotificationCategory>))
UIApplication.shared.registerForRemoteNotifications()
}
}
Hier ist, wo ich die lokale Benachrichtigung erstellen:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if UIApplication.shared.applicationState == UIApplicationState.active {
// Do something you want when the app is active
} else {
// Do something else when your app is in the background
}
// fire a local notification upon receiving FCM notification
let localNotification = UILocalNotification()
localNotification.alertTitle = "Notification Title"
localNotification.alertBody = "more details"
localNotification.alertAction = "ShowDetails"
localNotification.fireDate = NSDate().addingTimeInterval(5) as Date
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.applicationIconBadgeNumber = 1
localNotification.category = "BOOKING_CATEGORY"
UIApplication.shared.scheduleLocalNotification(localNotification)
print("Push notification received bkg: \(userInfo)")
print("completed" , completionHandler)
}
Jede Hilfe würde geschätzt.
Was wie Ihre Nutzlast für die Benachrichtigung sieht? Haben Sie eine Benachrichtigungsnutzlast, eine Datennutzlast oder beides? –
Danke für die Antwort, ich habe beide, ich implementiert die Funktion, die lokale Benachrichtigung in der App-Delegierten-Klasse erhält und es scheint, dass es jedes Mal aufgerufen wird. Mein Problem besteht also darin, eine lokale Benachrichtigung zu planen, wenn die App beendet wird oder im Hintergrund läuft und ich eine Remote-Benachrichtigung erhalte, da die App-Delegiertenfunktionen nicht aufgerufen werden, wenn die App beendet wird! – TheeBen