0

I know how to raise local notifications and show actions on it. When a user clicks on actions I can handle it on below method:

func userNotificationCenter(_ center: UNUserNotificationCenter, 
 didReceive response: UNNotificationResponse, withCompletionHandler 
 completionHandler: @escaping () -> Void) {
   // Do something here
    if response.notification.request.content.userInfo["backUp"] {
     doBackUpHere()
     // Do other methods
    }
}

However, my situation is a little different. I need to do some functions when a notification raised before clicking on it or on it's actions(application is foreground or background). Other methods run when the user clicks on notification

func notificationRaised (notification: Notification){
  // to do if notification is 
  if notification.userInfo["backUp"] {
    doBackUpHere()
  }
}

Update

Please see @Sergey answer and our comments to understand better my propose.

Amir Shabani
  • 690
  • 2
  • 14
  • 36

2 Answers2

1

If I understand your question correctly, you want to make an action when a notification arrives, without any user input. If so, I suggest you take a look at silent push notifications in iOS - they do just that, unfortunately - not showing in the user interface at all.

You could also experiment with local notifications on a timer, but remember that you can only run code when a user taps on a notification and/or one of its actions as the app is not guaranteed to be alive by the time a scheduled local notification is shown.

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120
  • Yes, your understand my question correctly. [Apple documentation](https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/BasicSupport.html) says it is only for server notification. I need local notification. I have to turn on push notification that is not my goal. – Amir Shabani Sep 06 '17 at 09:54
  • @AmirShabani got it. So basically you can't do anything here as you will never know if your app is alive or not when you **actually send the notification**. – Sergey Grischyov Sep 06 '17 at 12:26
  • You mean there is no way to understand when notification raised? It can help me to know when a notification raised. Like broadcast receiver in android – Amir Shabani Sep 06 '17 at 12:54
  • 1
    @AmirShabani here's a quick clue. If it's a local notification, you're scheduling it yourself and so - you always know **when** you scheduled it, you have this data and you can save it locally to your app. The other thing is - your app might not be alive at this moment when notification is presented, so this info is useless if you can't actually execute any code. – Sergey Grischyov Sep 06 '17 at 13:55
0

Have you heard about 'Silent push notifications'? I think it is exactly what you are looking for ;) You can get that notification, do your stuff, and then show a local notification if you want.

  • Thanks a lot. However, I found for using silent push notifications I need to use push notification that is not my goal. Do you have a good resource for silent push notification. – Amir Shabani Sep 06 '17 at 09:56