phpreact-nativeexpoexpo-notifications

How to Use exponent-server-sdk-php to send push notifications to an app


Please I need help in sending push notifications to anybody that installs my app on their android phone to get a push notification each time I create a post using PHP as the backend.

Here is my code for that:

$response = array();
require_once __DIR__.'/exponent-server-sdk-php/vendor/autoload.php';


        $channelName = 'video_alert';
        $recipient =  'ExponentPushToken[AAAAnrlyESE:APA91bHYeOkFi5MlUB9eitKn1yEuReJ1dNrRSnlMob0lcBLAjoanDaAIposGTBWQD164CGMV8F0hriJtsXuaFPdoKxEpjtrnXshoxlZQmxdlrCjP_F5TyRToNGP43YqwahCd6XYhKvwX]';
        
        // You can quickly bootup an expo instance
        $expo = \ExponentPhpSDK\Expo::normalSetup();
        
        // Subscribe the recipient to the server
        $expo->subscribe($channelName, $recipient);
        
        // Unsubscribe the recipient from the server
        $expo->unSubscribe($channelName, $recipient);
        
        // Build the notification data
        $notification = ['title' => 'Watch Movies', 'body' => 'Catch new latest movie ('.$movie_title.') on DooBuxx Movies today!', 'data' => json_encode(array('notification_type' => 'service_requested'))];
        try {
        // Notify an interest with a notification
        $expo->notify([$channelName], $notification);
        }  catch (ExpoException $e) {
            $response["message"] = $e->getMessage();
         }

For the following part:

$channelName = 'video_alert';
        $recipient =  'ExponentPushToken[Server key]'; // I Inserted the server key i got from my firebase dasboard here. The package name is ok

Is the server key the right thing? or what? This is where I think I'm not doing it right. But I don't know what else to put there.

As you can see, I'm using the exponent-server-sdk-php that I cloned from GitHub to send the push notification but it's not sending anything after posting to the server.

Is there anything I'm doing wrong here? I have also checked the error log, but there is no erro in there.

When I posted the first time, a token file was created though but no notification was sent to the android phones that have the app installed.

I have configured my firebase credentials for the react native project I'm building after following the instructions from the expo website here but all to no avail.

I know I'm not doing something all right here but can't seem to figure it out yet. Please help.


Solution

  • It's not the server key you should put there but a token to identify an user device. With Expo you can get it by running Notifications.getExpoPushTokenAsync(). It gives you a token that looks like ExponentPushToken[unique1] where unique 1 is an unique identifier. You should store it on your backend and call it where you want to send a notification to this specific device.

    Then you should subcribe a recipient for each token (device) you want to send a notification.

    Exemple from the doc :

    $channelName = 'video_alert';
    
    $recipient1 = 'ExponentPushToken[unique1]';
    $recipient2 = 'ExponentPushToken[unique2]';
    
    // …
    
    // Subscribe the recipients to the server
    $expo->subscribe($channelName, $recipient1);
    $expo->subscribe($channelName, $recipient2);
    
    // …
    
    // Notify an interest with a notification, the 2 recipients will receive it
    $expo->notify([$channelName], $notification);