swift - iOS FCM push notification not received if app is terminated -
i working on swift project , integrating fcm it. able receive push notification when app running when app in background state. when terminate (force close) app, on sending notification console, no notification shown.
i working on ios 10 , implemented below code in didfinishlaunchingwithoptions:
unusernotificationcenter.current().delegate = self unusernotificationcenter.current().requestauthorization(options: [.alert, .badge, .sound]) { granted, error in if error == nil { uiapplication.shared.registerforremotenotifications() if granted { print("notification access true!") } else { print("notification access false") } } }
i have implemented unusernotificationcenterdelegate , it's methods.
func usernotificationcenter(_ center: unusernotificationcenter, willpresent notification: unnotification, withcompletionhandler completionhandler: @escaping (unnotificationpresentationoptions) -> void) { completionhandler([.alert,.badge, .sound]) } func usernotificationcenter(_ center: unusernotificationcenter, didreceive response: unnotificationresponse, withcompletionhandler completionhandler: @escaping () -> void) { let userinfo = response.notification.request.content.userinfo print(userinfo) self.handleonnotifclick() completionhandler() }
it happens when app force closed used apps drawer. please it. appreciated.
solved!!!
func application(_ application: uiapplication, didreceiveremotenotification userinfo: [anyhashable: any],fetchcompletionhandler completionhandler: @escaping (uibackgroundfetchresult) -> void) { let state: uiapplicationstate = uiapplication.shared.applicationstate if state == .active{ if let notification = userinfo["aps"] as? [anyhashable: any], let alert = notification["alert"] as? string { print(alert) let localnotification = uilocalnotification() localnotification.alertbody = alert localnotification.soundname = uilocalnotificationdefaultsoundname uiapplication.shared.schedulelocalnotification(localnotification) } }else if state == .inactive{ if let notification = userinfo["aps"] as? [anyhashable: any],let alert = notification["alert"] as? string { print(alert) let localnotification = uilocalnotification() localnotification.alertbody = alert localnotification.soundname = uilocalnotificationdefaultsoundname uiapplication.shared.schedulelocalnotification(localnotification) } }else if state == .background{ uiapplication.shared.applicationiconbadgenumber = 0 if let notification = userinfo["aps"] as? [anyhashable: any],let alert = notification["alert"] as? string,let sound = notification["sound"] as? string{ print(alert) var localnotification = uilocalnotification() localnotification.alertbody = alert localnotification.soundname = sound uiapplication.shared.schedulelocalnotification(localnotification) } } }
Comments
Post a Comment