expo-notifications outside of an iOS app

Please provide the following:
Expo CLI 4.7.3

  1. SDK Version:
    SDKs:
    iOS SDK:
    Platforms: iOS 14.5, DriverKit 20.4, macOS 11.3, tvOS 14.5, watchOS 7.4
    npmPackages:
    expo: ^41.0.1 => 41.0.1
    react: 17.0.2 => 17.0.2
    react-native: 0.64.1 => 0.64.1
  2. Platforms(Android/iOS/web/all): iOS
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

Hello everyone,

How can I get the notifications to be shown when I´m outside the app (iOS) but it´s been difficult to achieve it. I´m using a react hook and I don’t know what I’m missing:

import Constants from 'expo-constants';
import React from 'react';
import {Platform} from 'react-native';
import * as Notifications from 'expo-notifications';

if (Platform.OS !== 'android') {
  Notifications.setNotificationHandler({
    handleNotification: async () => ({
      shouldShowAlert: true,
      shouldPlaySound: true,
      shouldSetBadge: true,
    }),
  });
}

const useLocaliOSNotificacion = () => {
  const [expoPushToken, setExpoPushToken] = React.useState('');
  const [notification, setNotification] = React.useState(false);
  const notificationListener = React.useRef();
  const responseListener = React.useRef();
  const navigationiOS = React.useRef(null);

  React.useEffect(() => {
    if (Platform.OS !== 'android') {
      registerForPushNotificationsAsync().then((token) =>
        setExpoPushToken(token),
      );

      notificationListener.current =
        Notifications.addNotificationReceivedListener((notification) => {
          setNotification(notification);
        });

      responseListener.current =
        Notifications.addNotificationResponseReceivedListener((response) => {
          console.log(response);
        });

      return () => {
        Notifications.removeNotificationSubscription(
          notificationListener.current,
        );
        Notifications.removeNotificationSubscription(responseListener.current);
      };
    }
  }, []);

  React.useEffect(() => {
    console.log('notification.data', notification?.data);
  }, [notification]);

  const registerForPushNotificationsAsync = async () => {
    let token;
    if (Constants.isDevice) {
      const {status: existingStatus} =
        await Notifications.getPermissionsAsync();
      let finalStatus = existingStatus;
      if (existingStatus !== 'granted') {
        const {status} = await Notifications.requestPermissionsAsync({
          ios: {
            allowAlert: true,
            allowBadge: true,
            allowSound: true,
            allowDisplayInCarPlay: true,
            allowCriticalAlerts: true,
            provideAppNotificationSettings: true,
            allowProvisional: true,
            allowAnnouncements: true,
          },
        });
        finalStatus = status;
      }
      if (finalStatus !== 'granted') {
        alert('Failed to get push token for push notification!');
        return;
      }
      token = (await Notifications.getExpoPushTokenAsync()).data;
      console.log(token);
    } else {
      alert('Must use physical device for Push Notifications');
    }

    if (Platform.OS === 'android') {
      Notifications.setNotificationChannelAsync('default', {
        name: 'default',
        importance: Notifications.AndroidImportance.MAX,
        vibrationPattern: [0, 250, 250, 250],
        lightColor: '#FF231F7C',
      });
    }

    return token;
  };

  const schedulePushNotification = async (fnNavigation, msg, usrinfo) => {
    if (navigationiOS.current == null) {
      navigationiOS.current = fnNavigation;
    }
    await Notifications.scheduleNotificationAsync({
      content: {
        title: usrinfo?.payload.placeParams.clinombre,
        body: msg,
        data: usrinfo,
        ios: {sound: true},
        sound: 'default',
      },
      trigger: {seconds: 2},
    });
  };

  const fn = Platform.OS !== 'android' ? schedulePushNotification : () => {};

  return [{fn}];
};

export default useLocaliOSNotificacion;

Thank you for your suggestions and help.

Hey @titanve, can you elaborate on what you mean by outside the app? In the Notif Center/Lock Screen, as an OS dropdown alert? This should work automatically without any additional configuration needed.

Cheers,
Adam

@adamjnav

Hi!

I mean when the app is in the background, I do get notifications when I’m inside the app, but I don’t when I’m outside, what can I do to get the notifications when I’m inside another app? Or in the home screen?

Thank you!

Got it. Yeah, this should work out of the box. Can you double check what the Notifications settings are in the Device Settings (Settings → Notifications → Your App)? Also, what device/iOS version are you testing on?

Hello @adamjnav

This is my config and I´m currently using an iPhone 11 with iOS version 14.6

What do you think it can be?

Thank you.

Hello @adamjnav

I’m using TestFlight in order to test my app, does it have something to do with it?

Thank you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.