PHP code example of guppylab / plugin-push

1. Go to this page and download the library: Download guppylab/plugin-push library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

guppylab / plugin-push example snippets


use Guppylab\Push\PushNotificationServiceProvider;

public function plugins(): array
{
    return [
        PushNotificationServiceProvider::class,
    ];
}

use Native\Mobile\Facades\PushNotifications;

PushNotifications::enroll();          // asks permission and enrolls the device
PushNotifications::checkPermission(); // granted|denied|not_determined|provisional
PushNotifications::getToken();        // last token received, or null

use Native\Mobile\Attributes\OnNative;
use Native\Mobile\Events\PushNotification\TokenGenerated;

#[OnNative(TokenGenerated::class)]
public function onToken(string $token, ?string $id = null): void
{
    // iOS: raw APNs device token in hex — what a backend posting to
    // api.push.apple.com with an ES256 (.p8) JWT needs.
    // Android: FCM registration token.
}

use Guppylab\Push\Events\MessageReceived;
use Guppylab\Push\Events\NotificationTapped;
use Native\Mobile\Attributes\OnNative;

#[OnNative(MessageReceived::class)]
public function onPush(MessageReceived $event): void
{
    $orderId = $event->get('order_id');
}

#[OnNative(NotificationTapped::class)]
public function onTap(NotificationTapped $event): void
{
    if ($event->link) {
        $this->redirect($event->link);
    }
}

use Guppylab\Push\Facades\Push;

Push::unenroll();   // delete the device token and stop receiving pushes
Push::setBadge(3);  // iOS only; reports success: false on Android
Push::clearBadge();
Push::isSupported(); // false on an iOS simulator or without google-services.json

// config/push.php
'channel' => [
    'id' => 'default',
    'name' => __('Notifications'), // shown in YOUR app's system settings
    'importance' => 'high',        // heads-up banner
],
'android' => [
    'small_icon' => 'ic_notification', // white, transparent background
    'color' => '#2563EB',
],
bash
composer native:plugin:register guppylab/plugin-push
php artisan native:run   # rebuild the native project
bash
php artisan vendor:publish --tag=push-config