Expo Push Notifications in background

I have a doubt regarding a point mentioned in expo docs:

Expo apps don’t support background code execution (running code when the app is not foregrounded or the device is sleeping). This means you cannot use background geolocation, play audio in the background, handle push notifications in the background, and more. This is a work in progress.

When the app is open and is in foreground or background and some other user sends him/her a chat message then the push notifications are working fine.

So what if the user has not opened the app yet then will he receive a push notification as soon as some other user sends this user a chat message?

Thanks in Advance!

Yes, the user will receive a push notification when the app is backgrounded. The docs are saying that you can’t do run any app specific code when the push notification is received and the app is backgrounded, but the user will still see it.

2 Likes

Hi there

I was just about to post about the same issue. Thanks @jesse for your contribution, I have a small follow-up question though:

In my current (Xamarin) app on Android, I actually get some JSON as a push message, which I then parse into a (localized) message displayed to the user. If the app isn’t running, it’s being started and I have the chance of handling the message. So you are saying this isn’t possible with expo?

Thanks!

@hardcodet, correct that’s not possible on Expo without detaching right now. We eventually want to support something like that but it might be a while.

1 Like

Hi @jesse …thanks for the clarification, I was very close to ditching Expo but I read your comment, now I’m sticking with it. It may be worth clarifying this in the “Why Not Expo” documentation

1 Like

thank you @jesse for this info. But please i wanna know if there is a different way of making the push notification work before opening the app or is it just the usual way.

Summary

I have installed expo-task-manager in my custom client to be able run tasks in response to notifications received in the app. On Android when a notification is sent the background test function fires while the app is in the background, but not on iOS. I have added “UIBackgroundModes”: [“remote-notification”] to my infoPlist in my app.json. I am even able to confirm the task has actually registered when I console log it using the code below on iOS

const test = await TaskManager.getRegisteredTasksAsync()
console.log(test)

Not sure why the task is not running. I am successfully receiving the notifications from the expo notification tool but they are not triggering the background task on iOS.

Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!

managed

What platform(s) does this occur on?

iOS

SDK Version (managed workflow only)

42

Environment

  Expo CLI 4.7.3 environment info:
    System:
      OS: macOS 10.15.7
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 14.15.1 - /usr/local/bin/node
      Yarn: 1.22.10 - /usr/local/bin/yarn
      npm: 7.8.0 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    Managers:
      CocoaPods: 1.10.1 - /usr/local/bin/pod
    SDKs:
      iOS SDK:
        Platforms: iOS 14.4, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2
    IDEs:
      Android Studio: 4.1 AI-201.8743.12.41.6858069
      Xcode: 12.4/12D4e - /usr/bin/xcodebuild
    npmPackages:
      expo: ^42.0.0 => 42.0.0 
      react: 16.13.1 => 16.13.1 
      react-dom: 16.13.1 => 16.13.1 
      react-native: https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz => 0.63.2 
      react-native-web: ~0.13.12 => 0.13.18 
    npmGlobalPackages:
      expo-cli: 4.7.3
    Expo Workflow: managed

Reproducible demo or steps to reproduce from a blank project

add expo-task-manager to project
add expo notifications to project
add the following to your app.json

 "ios": {
            "infoPlist": {
                "UIBackgroundModes": ["remote-notification"]
            },

use code bellow to a test task. and then use the expo notification tool to send a notification to your app while it is in the background.

import * as Notifications from 'expo-notifications'
import * as TaskManager from 'expo-task-manager'

const BACKGROUND_NOTIFICATION_TASK = 'BACKGROUND-NOTIFICATION-TASK'

TaskManager.defineTask(
    BACKGROUND_NOTIFICATION_TASK,
    ({ data, error, executionInfo }) => {
        console.log('Received a notification in the background!')
        console.log(data)
        // Do something with the notification data
    }
)

Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK)
1 Like

It may not work with ‘expo-notifications’, but try: @react-native-firebase/app and @react-native-firebase/messaging.