0

I am trying to get push notifications working in a ios React Native app and am half way there. The one problem im having is that my logic for displaying a Notification while the app is in the background doesn't execute until the app becomes active again. I am using Socket.io and listening for events, but the code to handle (say onChat event) doesn't run until the app is active. I have verified that the socket is still connected.

The one things thats weird is that is works in the simulator but not on my device. Maybe there is performance optimizations that halt JS execution while the app is put in the background.

How can i get my JS to execute while the appstate is in the background?

What im doing is calling a function when the event is recieved over the socket and it runs:

PushNotificationIOS.presentLocalNotification({
  alertBody: `New Chat: ${chat.message}`,
});
Russell Maxfield
  • 968
  • 7
  • 14

1 Answers1

1

Thanks to this stack overflow question and answers I was able to find out that code is not executed in the background state to prepare it for the suspended state. The background mode is a transition state to prepare the app to be "screen shotted". The socket will stay connected only for a short time there after, but would eventually get terminated.

In the case with doing a push notifications, I would need to turn that over to Parse or some other service to push a notification to my app while the app is in the background.

Russell Maxfield
  • 968
  • 7
  • 14