3

I'm working on mobile app using expo managed workflow. My app has PHP REST API for the backend. I encountered a strange issue when working on the push notifications part of the app.

I'm using 2 different devices for testing, an iPhone and Android phone. Both devices are using different user accounts to login in the app and both devices are setting different expo push notification tokens in the db.

When the admin wants to send a new offer to specific user the push notification is received by both devices instead only the device that belongs to the user to which the admin wants to send a message.

Here is how I'm sending the notification from the backend. This code is executed only once, so I guess I'm doing something wrong here.

$this->expo = Expo::normalSetup();
$interest = 'new_offer';

// Subscribe the recipient to the server
$this->expo->subscribe($interest, $recipient);

// Build the notification data
$notification = [
   'title' => $this->title,
   'body' => $body,
   'data' => json_encode([
      'type' => $this->type,
      'inquiryId' => $this->inquiry->getId(),
      'offerId' => $this->offer->getId(),
      ]),
   ];

// Notify an interest with a notification
$this->expo->notify($interest, $notification);

And here is how I'm handling it in the app

    /**
     * Get Expo push token and connect it with the user
     * @see https://docs.expo.io/versions/latest/guides/push-notifications/
     * @returns {Promise<void>}
     */
    registerForPushNotificationsAsync = async () => {
        const {status: existingStatus} = await Permissions.getAsync(Permissions.NOTIFICATIONS);
        let finalStatus = existingStatus;

        // only ask if permissions have not already been determined, because
        // iOS won't necessarily prompt the user a second time.
        if (existingStatus !== 'granted') {
            // Android remote notification permissions are granted during the app
            // install, so this will only ask on iOS
            const {status} = await Permissions.askAsync(Permissions.NOTIFICATIONS);
            finalStatus = status;
        }

        // Stop here if the user did not grant permissions
        if (finalStatus !== 'granted') {
            return;
        }

        // Get the token that uniquely identifies this device
        const token = await Notifications.getExpoPushTokenAsync();

        /**
         * @token ExponentPushToken[cszdf-SKFcXkd-8e6BEtjNG]
         */

        this.pushNotificationsToken = token;
        this._notificationSubscription = Notifications.addListener(this._handleNotification);
    }

    _handleNotification = ({origin, data}) => {
        console.warn(origin, data);

        switch (data.type) {
            case PushNotificationTypes.PARTNER_SUBMITTED_NEW_OFFER: {
                return this.props.navigation.navigate('InquiryOffers', {getInquiryId: data.inquiryId});
            }
        }
    }

Any ideas what I'm doing wrong and how to fix it?

Thanks,

Bob

eazybob
  • 109
  • 2
  • 10

0 Answers0