Push notification is not working when app is in background (minimized) - IOS
Push notification is not working when app is in background (minimized) - IOS
I'm newbie to Swift, i am creating chat application, i need to send notification when app is in foreground or minimized.
but i am not getting the notification when app is minimized (it works when USB is connected.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
Messaging.messaging().appDidReceiveMessage(userInfo)
let action = userInfo["action"] as! String
let notification = UILocalNotification()
notification.fireDate = NSDate() as Date
notification.alertTitle = "test"
notification.alertBody = "test"
notification.alertAction = "Ok"
UIApplication.shared.applicationIconBadgeNumber = 1
UIApplication.shared.scheduleLocalNotification(notification)
completionHandler(UIBackgroundFetchResult.newData)
}
Notification code:
@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
center.removeAllDeliveredNotifications() // to remove all delivered notifications
center.removeAllPendingNotificationRequests() // to remove all pending notifications
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: (messageID)")
}
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([.alert, .sound, .badge])
}
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[gcmMessageIDKey] {
print("Message ID: (messageID)")
}
print(userInfo)
completionHandler()
}
}
FCM Payload:
{
"to" : "/topics/channel_18",
"data" : {
"action" : "NOTIFY",
"message" : "{"text":"test" }"
},
"content_available" : true
}
I have tried with Priority high and with Sound option but none works.
Please note that i am not using "notification" key as per client request.
Please anyone help me to work notification when app is in background without USB connection.
Looking for an answer drawing from credible and/or official sources.
1. "but i am not getting the notification when app is minimized (it works when USB is connected." what do you mean by this? 2. Are you testing on a physical device or you're using the simulator?
– Honey
yesterday
i am using physical device
– US-1234
yesterday
notification is not working when screen is locked
– US-1234
yesterday
does it work when the app is in the background? ie Just pressing home and not locking the phone?
– Honey
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Just to be clear: it works when app is in forgeround? but just doesn't work when you press home button?
– Honey
yesterday