Custom actions while app in background

Hi there,

We're currently using custom actions for storing a boolean in UserDefaults and then showing a prompt to the user.

Those actions are fired through silent notifications for given named users and they are working fine while the app is opened, regardless of it's in the foreground or background.

Nevertheless, they don't work if the app is closed. I'm checked that silent notifications fire application:didReceiveRemoteNotification:fetchCompletionHandler: method in the app delegate but the action is not performed. Am I not initializing the push handler properly? Should I launch the action manually? There's another feature that fits better for the desired behavior?

Thanks for your help!

Didn't find what you were looking for?

New post

Comments

7 comments

  • Omar,

    For notifications or payloads that are received while the app is in the background, there are different iOS methods that fire for those situations.

    Instead of didReceiveRemoteNotification firing, you'll instead have a receivedBackgroundNotification method that would allow you to perform some action in the background.

    I would recommend setting up a Push Handler delegate as this will allow you access to that method to store your bool value when the app receives a silent notification in the background.

    Make sure, also, that your app is set up to receive background notifications and is turned on within your device settings to ensure that that method fires.

    Comment actions Permalink
    0
  • Hi Michael,

    I already have a singleton that works as push handler and has both receivedBackgroundNotification and receivedForegroundNotification, but the first one is never called. Could it be due to the handler being a singleton?

    Anyway, can custom actions be launched by background notifications or should I do it manually in receivedBackgroundNotification?

    Thanks for your response, Michael!

    Comment actions Permalink
    0
  • Omar,

    I definitely wouldn't set the Push Handler as a singleton. Setting the Urban Airship handler to a delegate is the way to go to allow the SDK to handle those pushes through your delegate.

    Any action really can take place in the background, whether it be downloading data or storing values for use the next time the user opens the app. You would need to do this in the receivedBackgroundNotification method though since there is no other way to trigger actions to fire without some sort of external trigger. That is, some code needs to run when a background notification is received.

    Comment actions Permalink
    0
  • Hi Michael,

    Sorry for the delay but I was busy with other features in the app.

    I've been changing my push handler, even using the one you have in your SwiftSample project, and I haven't been able to fire receivedBackgroundNotification after sending a silent notification with the app closed while application:didReceiveRemoteNotification:fetchCompletionHandler: has received all of them, regardless of user accepted receiving push notifications or not.

    Am I missing something? The configuration works because I'm receiving normal notifications without problems. I have also activated Background fetch mode.

    Comment actions Permalink
    0
  • Omar,

    Since you mentioned the SwiftSample, do you happen to know if you can reproduce the same behavior with the sample app? That is, do any of the PushHandler methods get fired at all from the sample?

    Comment actions Permalink
    0
  • Michael, I had the app delegate implemented as UNUserNotificationCenterDelegate for other local notifications I have and that stopped some of Urban Airship delegate methods to be fired.

    All seems to be working as expected now, thank you so much for your help!

    Comment actions Permalink
    0
  • Omar,

    No problem, happy to help!

    Comment actions Permalink
    0

Please sign in to leave a comment.