I have implemented push notification and subscribe app on different news topics. When a silent push notification received i want my app to subscribe/unsubscribe the topic on type based. But problem i am facing is when app is in background code executed and hit the subscribe/unsubscribe code but app wont process it and as soon as i open the app i got message that topic has been subscribed/unsubscribe successfully. While in foreground code is executing and processing successfully. Background processing and Background fetch are enabled in background modes. My Code is as:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler
completionHandler: @escaping (UIBackgroundFetchResult)
-> Void) {
if let messageID = userInfo[gcmMessageIDKey] {
print("/// Message ID: \(messageID)")
}
if let topic = userInfo["topic"] as? String{
if let type = userInfo["type"] as? String{
do {
if(type == "unsubscribe"){
Messaging.messaging().unsubscribe(fromTopic: topic)
} else {
Messaging.messaging().subscribe(toTopic: topic)
}
}catch{
}
}
}
// Print full message.
print("info = ", userInfo)
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
My Headers from postman are
apns-push-type:background
apns-priority:10
and the body
{
"to" : "*** device token ***",
"priority" : "high",
"content_available": true,
"data" : {
"topic" : "Sports",
"type" : "unsubscribe" // subscribe
}
}
What i want is to subscribe/unsubscribe the fcm topics when app is in background and silent notification received.