0

When my app is killed I am not receiving push notification, but is working fine with the background. After receiving notification I am generating a local notification by getting data from my realm DB. In payload content available key is 1. Please suggest me where I am going wrong, with getting fcm notification or running in the background.

here is my code -

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    let state: UIApplicationState = UIApplication.shared.applicationState

    print(userInfo)
    if state == .active{
        let messageBody = userInfo["aps"] as! Dictionary<String,Any>
        print("@@@@@@@@@@@@@ got notification")
        if let message = messageBody["message"] {
            let messageDict = DCUtilityClass.convertToDictionary(text: message as! String)
            let _ = DCPushNotificationAPI.getNotificationResult(info: (messageDict)!)
        }
    } else if state == .inactive {
        let messageBody = userInfo["aps"] as! Dictionary<String,Any>
        print("@@@@@@@@@@@@@ got notification")
        if let message = messageBody["message"] {
            let messageDict = DCUtilityClass.convertToDictionary(text: message as! String)
            let _ = DCPushNotificationAPI.getNotificationResult(info: (messageDict)!)
        }
    } else if state == .background {
        let messageBody = userInfo["aps"] as! Dictionary<String,Any>
        print("@@@@@@@@@@@@@ got notification")
        if let message = messageBody["message"] {
            let messageDict = DCUtilityClass.convertToDictionary(text: message as! String)
            let _ = DCPushNotificationAPI.getNotificationResult(info: (messageDict)!)
        }
    }

    completionHandler(UIBackgroundFetchResult.noData)
    //receiveMessageMethod(recieveMessage: messageBody as! Dictionary<String, Any>)
}

and

class func triggerLocalPushNotification(title:String, body:Dictionary<String,Any>) {


    //creating the notification content
    let content = UNMutableNotificationContent()

    //adding title, subtitle, body and badge

    let messageDictModel = DCChatListHistoryModel.init().initWithUserChatDictionary(userDictionary: body)

    if messageDictModel.mIsGroup{
        if messageDictModel.mGroupDetailsModelArray.count == 0 {
            let groupData =  DCDBDataUtilityClass.getGroupInfoByConversationId(id: messageDictModel.m_id)
            content.title = (groupData?.mGroupsModelArray.first?.mGroup_name)!
            content.body = messageDictModel.mlatest_message
        } else {
            content.title = (messageDictModel.mGroupDetailsModelArray.first?.mGroup_name)!
            content.body = messageDictModel.mlatest_message
        }

    } else {
        var messageSender = ""
        if messageDictModel.mMessageFrom == UserDefaults.standard.value(forKey: "user_id") as! String {
            messageSender = messageDictModel.mMessage_to
        } else {
            messageSender = messageDictModel.mMessageFrom
        }
        let name = DCDBDataUtilityClass.getInfoById(id: messageSender)
        content.title = (name?.mFull_name)!
        content.body = messageDictModel.mlatest_message
    }
    //getting the notification trigger
    //it will be called after 5 seconds
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.2, repeats: false)

    //getting the notification request
    let request = UNNotificationRequest(identifier: "iOSNotification", content: content, trigger: trigger)

    //adding the notification to notification center
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

}
KENdi
  • 7,576
  • 2
  • 16
  • 31
Khushboo Dhote
  • 198
  • 3
  • 13
  • By not receiving push notification, you mean that you do not get any notification in the notification tray or just that you are not getting "@@@@@@@ got notification" printed on the console? – kerry Oct 09 '18 at 05:18
  • When the app is in the background or not running, notification will be delivered to Notification Center, app will receive them only after users interaction with it. – Satish Oct 09 '18 at 05:59
  • @kerry my local notification is not getting generated. – Khushboo Dhote Oct 09 '18 at 11:06
  • @Satish I am getting the notification in foreground and background but when I will kill the app I will not be getting. – Khushboo Dhote Oct 09 '18 at 11:09
  • @KhushbooDhote - how and from where `triggerLocalPushNotification` will be invoked? – Satish Oct 09 '18 at 12:32
  • AFAIK, the iOS system itself prevents any app (unless native iOS apps) to run services if the app is intentionally *killed*. Possibly helpful post [here](https://stackoverflow.com/q/51302525/4625829) – AL. Oct 09 '18 at 17:38
  • @Satish after processing userInfo data in DCPushNotificationAPI.getNotificationResult(info: (messageDict)!) function. – Khushboo Dhote Oct 12 '18 at 07:26
  • Hello, did you ever manage to solve this issue? – Andrei F Nov 02 '18 at 09:32

0 Answers0