Reputation: 1242
In my application I have implemented FCM push notification. It is working fine When checking notification with FCM console and pushtry.com website, Then I tried with actual Server API the foreground notification works well, But background notification not receiving and sometimes it receives very rarely and that too without banner and sound . Please help to find out the issue.
Here I attached the code what I am trying..
import UIKit
import UserNotifications
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
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)
}
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print(userInfo)
}
func applicationDidBecomeActive(_ application: UIApplication)
{
Messaging.messaging().shouldEstablishDirectChannel = true
application.applicationIconBadgeNumber = 0;
}
func applicationDidEnterBackground(_ application: UIApplication)
{
Messaging.messaging().shouldEstablishDirectChannel = false
print("Disconnected from FCM.")
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping
(UIBackgroundFetchResult) -> Void) {
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \ .
(error.localizedDescription)")
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs token retrieved: \(deviceToken)")
}
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
print(userInfo)
completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler:
@escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print(userInfo)
completionHandler()
}
}
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didRefreshRegistrationToken
fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage:
MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
}
Here I share the request format from server for push the notification:
{
"to":"cerGBjsmtzE:APA91bFk-
6ZI4ehaWbg0bGSGzAh10NeUh3AcyEFq7dASU7W4YY4WL8vWCA5wav-
LTYc0xTGWHev8Z99",
"priority":"high",
"data":
{
"title":"New Inspection scheduled",
"body":"You have a new Inspection Request scheduled,
"sound":"default"
}
}
Upvotes: 0
Views: 5975
Reputation: 317
To receive the push notification in background mode there must be "mutable-content"
key with value "1"
or true
is present in your payload format.
Please tell you, backend developer, to add that key. Please check following valid payload format
{
"to": "dWB537Nz1GA:APA91bHIjJ5....",
"content_available": true,
"mutable_content": true,
"data":
{
"message": "Offer!",
"mediaUrl": "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/FloorGoban.JPG/1024px-FloorGoban.JPG"
},
"notification":
{
"body": "Enter your message",
"sound": "default"
}
}
You can refer the following link https://firebase.google.com/docs/cloud-messaging/concept-options
Upvotes: 3
Reputation: 948
Make sure you have selected the Remote settings in background mode --- Under the capabilities section of XCODE
Upvotes: 5