How to use Expo Push Token with Laravel-Exponent-Push-Notifications?

Please provide the following:

  1. SDK Version: 37
  2. Platforms: (Android/iOS)

We are building an app with Expo and We are trying to enable push notifications with our backend service which is written with php (Laravel), We are working with Laravel-Exponent-Push-Notifications

The following code snippet is used to send Notification with in Laravel project:

<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use NotificationChannels\ExpoPushNotifications\ExpoChannel;
use NotificationChannels\ExpoPushNotifications\ExpoMessage;
class Notification_app extends Notification
{
    use Queueable;

    public function __construct()
    {
        //
    }

    public function via($notifiable)
    {
        return [ExpoChannel::class];
    }

    public function toExpoPush($notifiable)
    {
       return ExpoMessage::create()
           ->badge(1)
           ->enableSound()
           ->title("Congratulations!")
           ->body("Your {$notifiable->service} account was approved!");
    }

    public function toArray($notifiable)
    {
       return [
           //
       ];
    }
}

Then calling the function to send notification like so $data->notify(new Notification_app());

On the client side the code is looks like the following:

useEffect(() => {
    registerForPushNotificationsAsync();
    Notifications.addListener(_handleNotification);
},
[])

const registerForPushNotificationsAsync = async () => {
    console.log(Constants.isDevice);
    if (Constants.isDevice) {
        const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
        let finalStatus = existingStatus;
        if (existingStatus !== 'granted') {
            const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
            finalStatus = status;
        }
        console.log(finalStatus);
        if (finalStatus !== 'granted') {
            alert('Failed to get push token for push notification!');
            return;
        }
        let token = await Notifications.getExpoPushTokenAsync();
        setExpoPushToken(token);
    } else {
        alert('Must use physical device for Push Notifications');
    }

    if (Platform.OS === 'android') {
        Notifications.createChannelAndroidAsync('default', {
            name: 'default',
            sound: true,
            priority: 'max',
            vibrate: [0, 250, 250, 250],
        });
    }
};

const _handleNotification = notification => {
    console.log(notification);
    Vibration.vibrate();
    if(notification.origin == 'selected'){
        props.navigation.navigate('MyTaskDetails');
    }
    setNotification();
};

Here we generate Expo push token then I know that we have to send it to backend so that each token will be related to a device which have our app installed, My question here is how to use device token (Expo Push token) in the above Laravel code to send notification to a specific device there is no parameters that refer to any token … any help or snippet of code that might help me here is appreciated.

I got the idea that We create a table called ‘exponent_push_notifiation_interests’ by migration, The question here is this is the table that will hold push key? … I accessed created table and I found two columns key and value and if so how could I access token to use it within code snippet mentioned in the package ReadMe.

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