ios - How to save message from push notification even if user does not tap the notification? -


i'm working swift 3 in xcode version 8.1 (8b62).

have been stuck on long time: when send push notification data through app i'm building , app in foreground, works should. when app in background , tap push notification comes in, works fine. however, whenever open app without tapping notification, message doesn't saved. problem not when user doesn't tap notification, when multiple notifications come in, user able tap 1 of them, , rest lost.

my app messaging app saves messages on phone (not in central database pull there update every time app comes foreground).

below code i'm using inside of appdelegate:

func application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplicationlaunchoptionskey: any]?) -> bool {     // override point customization after application launch.      firapp.configure()      let notificationtypes : uiusernotificationtype = [uiusernotificationtype.alert, uiusernotificationtype.badge, uiusernotificationtype.sound]     let notificationsettings = uiusernotificationsettings(types: notificationtypes, categories: nil)     application.registerforremotenotifications()     application.registerusernotificationsettings(notificationsettings)      return true } func application(_ application: uiapplication, didreceiveremotenotification data: [anyhashable : any]){     let state = uiapplication.shared.applicationstate      if state == .background {         print("i'm in background place")         updatenewmessage(data: data)     }     else if state == .active {         print("i'm in active place")         updatenewmessage(data: data)     }     else if state == .inactive{         print("i'm in inactive statement")         updatenewmessage(data: data)     }     else{         print("i'm in else statement")         updatenewmessage(data: data)     }  } 

the updatenewmessage(data: data) function what's necessary upload message data internal sql database of phone.

also few things happen: when message comes in while phone in foreground, print statement "i'm in active place" shows up, when user taps on notification while screen off or app in background "i'm in inactive statement" prints out.

from i've been reading, people saying way go here use in addition didreceiveremotenotification,

fetchcompletionhandler completionhandler: @escaping (uibackgroundfetchresult) -> void 

however when trying use together, i'm not able upload message received when user taps notification (unless i'm doing wrong...)

any appreciated, thank you!


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -