### Install Pusher Push Notifications PHP SDK Source: https://context7.com/pusher/push-notifications-php/llms.txt Install the SDK using Composer. This command adds the necessary Pusher push notifications library to your project. ```bash composer require pusher/pusher-push-notifications ``` -------------------------------- ### Install Pusher Push Notifications Composer Package Source: https://github.com/pusher/push-notifications-php/blob/master/README.md Use Composer to install the pusher/pusher-push-notifications package. This library requires PHP 8.0 or greater and the JSON PHP module. ```bash $ composer require pusher/pusher-push-notifications ``` -------------------------------- ### Configure Pusher Beams SDK Source: https://github.com/pusher/push-notifications-php/blob/master/README.md Initialize the PushNotifications client with your instance ID and secret key. Ensure you have included the Composer autoloader. ```php "YOUR_INSTANCE_ID_HERE", "secretKey" => "YOUR_SECRET_HERE", )); ``` -------------------------------- ### SDK Configuration Source: https://context7.com/pusher/push-notifications-php/llms.txt Initialize the PushNotifications client with your Pusher Beams instance ID and secret key. Optionally, you can specify a custom endpoint. ```APIDOC ## SDK Configuration Initialize the PushNotifications client with your Pusher Beams instance credentials. The client requires an `instanceId` and `secretKey` from your Pusher dashboard. ### Request Body - **instanceId** (string) - Required - Your Pusher Beams instance ID. - **secretKey** (string) - Required - Your Pusher Beams secret key. - **endpoint** (string) - Optional - A custom endpoint for testing or enterprise deployments. ### Request Example ```php "YOUR_INSTANCE_ID_HERE", "secretKey" => "YOUR_SECRET_KEY_HERE", ]); // Optionally specify a custom endpoint (for testing or enterprise deployments) $pushNotifications = new PushNotifications([ "instanceId" => "YOUR_INSTANCE_ID_HERE", "secretKey" => "YOUR_SECRET_KEY_HERE", "endpoint" => "https://custom-endpoint.example.com", ]); ?> ``` ``` -------------------------------- ### Initialize PushNotifications Client Source: https://context7.com/pusher/push-notifications-php/llms.txt Initialize the PushNotifications client with your Pusher Beams instance ID and secret key. You can optionally specify a custom endpoint for testing or enterprise deployments. ```php "YOUR_INSTANCE_ID_HERE", "secretKey" => "YOUR_SECRET_KEY_HERE", ]); ``` ```php "YOUR_INSTANCE_ID_HERE", "secretKey" => "YOUR_SECRET_KEY_HERE", "endpoint" => "https://custom-endpoint.example.com", ]); ``` -------------------------------- ### Handle Validation and API Errors in PHP Source: https://context7.com/pusher/push-notifications-php/llms.txt This snippet demonstrates how to catch and display exceptions for various validation errors, such as empty interests, too many interests, invalid interest names, and long user IDs. It also shows how to handle general API errors. ```php "a11aec92-146a-4708-9a62-8c61f46a82ad", "secretKey" => "EIJ2EESAH8DUUMAI8EE3KL5MNP7QRS9T", ]); // Validation errors are thrown as exceptions try { // Error: Empty interests array $pushNotifications->publishToInterests([], ["apns" => []]); } catch (Exception $e) { echo $e->getMessage(); // "Publishes must target at least one interest" } try { // Error: Too many interests (max 100) $interests = array_map(fn($i) => "interest-".$i, range(1, 101)); $pushNotifications->publishToInterests($interests, ["apns" => []]); } catch (Exception $e) { echo $e->getMessage(); // "Number of interests exceeds maximum of 100" } try { // Error: Invalid characters in interest name $pushNotifications->publishToInterests(["/invalid/interest"], ["apns" => []]); } catch (Exception $e) { echo $e->getMessage(); // "Interest contains a forbidden character..." } try { // Error: User ID too long (max 164 characters) $longUserId = str_repeat("A", 165); $pushNotifications->publishToUsers([$longUserId], ["apns" => []]); } catch (Exception $e) { echo $e->getMessage(); // "User id is longer than the maximum length..." } // API errors include error type and description try { $pushNotifications->publishToInterests(["test"], []); } catch (Exception $e) { // Format: "error_type: error_description" echo $e->getMessage(); } ``` -------------------------------- ### Publish Notifications to Specific Users in PHP Source: https://context7.com/pusher/push-notifications-php/llms.txt Send push notifications directly to a list of authenticated users. Supports up to 1000 user IDs per publish request. Ensure the PushNotifications client is initialized with your instance ID and secret key. ```php "a11aec92-146a-4708-9a62-8c61f46a82ad", "secretKey" => "EIJ2EESAH8DUUMAI8EE3KL5MNP7QRS9T", ]); try { // Send notification to specific authenticated users $publishResponse = $pushNotifications->publishToUsers( ["user-0001", "user-0002", "user-0003"], // Array of user IDs (max 1000) [ // iOS notification "apns" => [ "aps" => [ "alert" => [ "title" => "Order Update", "body" => "Your order #12345 has been shipped!" ], "sound" => "default" ] ], // Android notification "fcm" => [ "notification" => [ "title" => "Order Update", "body" => "Your order #12345 has been shipped!" ], "data" => [ "order_id" => "12345", "tracking_url" => "https://tracking.example.com/12345" ] ] ] ); echo "Published to users with ID: " . $publishResponse->publishId . "\n"; // Output: Published to users with ID: pub-5678 } catch (Exception $e) { echo "Failed to publish: " . $e->getMessage() . "\n"; } ?> ``` -------------------------------- ### Generate Token API Source: https://context7.com/pusher/push-notifications-php/llms.txt Generate a JWT authentication token for a user to enable Authenticated Users functionality on the client side. The token is valid for 24 hours. ```APIDOC ## POST /generateToken ### Description Generate a JWT authentication token for a user to enable Authenticated Users functionality on the client side. The token is valid for 24 hours and is signed with your secret key using HS256 algorithm. ### Method POST ### Endpoint /generateToken ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **userId** (string) - Required - The ID of the user for whom to generate the token. ### Request Example ```json { "userId": "user-0001" } ``` ### Response #### Success Response (200) - **token** (string) - The generated JWT authentication token. #### Response Example ```json { "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." } ``` ``` -------------------------------- ### POST /publishToInterests Source: https://context7.com/pusher/push-notifications-php/llms.txt Broadcast push notifications to groups of devices subscribed to specific interests. Supports up to 100 interests per publish with interest names up to 164 characters. ```APIDOC ## POST /publishToInterests Broadcast push notifications to groups of devices subscribed to specific interests. Device Interests allow you to send messages to all devices that have subscribed to a topic. Supports up to 100 interests per publish with interest names up to 164 characters. ### Method POST ### Endpoint /publishToInterests ### Parameters #### Request Body - **interests** (array) - Required - An array of interests to publish to (max 100 interests). - **payload** (object) - Required - An object containing platform-specific notification payloads. - **apns** (object) - Optional - Apple Push Notification Service (APNS) payload. - **aps** (object) - Required - The APNS alert dictionary. - **alert** (object) - Optional - The alert content. - **title** (string) - Optional - The title of the alert. - **body** (string) - Optional - The body of the alert. - **sound** (string) - Optional - The sound to play. - **badge** (integer) - Optional - The badge count for the app icon. - **fcm** (object) - Optional - Firebase Cloud Messaging (FCM) payload. - **notification** (object) - Optional - The FCM notification payload. - **title** (string) - Optional - The title of the notification. - **body** (string) - Optional - The body of the notification. - **data** (object) - Optional - Custom data to send with the notification. ### Request Example ```php "a11aec92-146a-4708-9a62-8c61f46a82ad", "secretKey" => "EIJ2EESAH8DUUMAI8EE3KL5MNP7QRS9T", ]); try { // Send notification to multiple interests with platform-specific payloads $publishResponse = $pushNotifications->publishToInterests( ["donuts", "promotions", "news"], // Array of interests (max 100) [ // iOS notification payload (APNS) "apns" => [ "aps" => [ "alert" => [ "title" => "New Promotion!", "body" => "Get 20% off all donuts today!" ], "sound" => "default", "badge" => 1 ] ], // Android notification payload (FCM) "fcm" => [ "notification" => [ "title" => "New Promotion!", "body" => "Get 20% off all donuts today!" ], // Include custom data for the app to process "data" => [ "promo_code" => "DONUT20", "expires_at" => "2024-12-31", "deep_link" => "/promotions/donut20" ] ] ] ); echo "Successfully published with ID: " . $publishResponse->publishId . "\n"; // Output: Successfully published with ID: pub-1234 } catch (Exception $e) { echo "Error publishing notification: " . $e->getMessage() . "\n"; } ?> ``` ### Response #### Success Response (200) - **publishId** (string) - The ID of the published notification. #### Response Example ```json { "publishId": "pub-1234" } ``` ``` -------------------------------- ### Publish to Users API Source: https://context7.com/pusher/push-notifications-php/llms.txt Send push notifications directly to specific authenticated users using their user IDs. Supports up to 1000 users per publish. ```APIDOC ## POST /publishToUsers ### Description Send push notifications directly to specific authenticated users. This method enables secure, targeted messaging to individual users identified by their user IDs. Supports up to 1000 users per publish with user IDs up to 164 characters. ### Method POST ### Endpoint /publishToUsers ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **users** (array) - Required - An array of user IDs to send the notification to (max 1000). - **notification** (object) - Required - An object containing the notification payload for different platforms (e.g., APNS, FCM). - **apns** (object) - Optional - APNS notification payload. - **fcm** (object) - Optional - FCM notification payload. ### Request Example ```json { "users": ["user-0001", "user-0002", "user-0003"], "notification": { "apns": { "aps": { "alert": { "title": "Order Update", "body": "Your order #12345 has been shipped!" }, "sound": "default" } }, "fcm": { "notification": { "title": "Order Update", "body": "Your order #12345 has been shipped!" }, "data": { "order_id": "12345", "tracking_url": "https://tracking.example.com/12345" } } } } ``` ### Response #### Success Response (200) - **publishId** (string) - The ID of the published notification. #### Response Example ```json { "publishId": "pub-5678" } ``` ``` -------------------------------- ### Delete User and Devices from Pusher Beams in PHP Source: https://context7.com/pusher/push-notifications-php/llms.txt Remove a user and all their associated devices from Pusher Beams. This is useful for user account deletion, logout cleanup, or GDPR compliance. Call this method with the user ID to be removed. ```php "a11aec92-146a-4708-9a62-8c61f46a82ad", "secretKey" => "EIJ2EESAH8DUUMAI8EE3KL5MNP7QRS9T", ]); try { // Delete user and all their registered devices $pushNotifications->deleteUser("user-0001"); echo "User successfully deleted from Pusher Beams\n"; } catch (Exception $e) { echo "Failed to delete user: " . $e->getMessage() . "\n"; } // Example: Cleanup on user account deletion function handleUserDeletion($userId) { global $pushNotifications; // Delete from your database first // $database->deleteUser($userId); // Then cleanup Pusher Beams try { $pushNotifications->deleteUser($userId); return true; } catch (Exception $e) { // Log error but don't fail the main operation error_log("Pusher Beams cleanup failed: " . $e->getMessage()); return false; } } ?> ``` -------------------------------- ### Delete User API Source: https://context7.com/pusher/push-notifications-php/llms.txt Remove a user and all their associated devices from Pusher Beams. Useful for user account deletion, logout cleanup, or GDPR compliance. ```APIDOC ## DELETE /users/{userId} ### Description Remove a user and all their associated devices from Pusher Beams. This is useful for user account deletion, logout cleanup, or GDPR compliance. ### Method DELETE ### Endpoint /users/{userId} ### Parameters #### Path Parameters - **userId** (string) - Required - The ID of the user to delete. #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) Indicates successful deletion. No specific response body is detailed. #### Response Example None provided. ``` -------------------------------- ### Generate JWT Token for Authenticated Users in PHP Source: https://context7.com/pusher/push-notifications-php/llms.txt Generate a JWT authentication token for a user, valid for 24 hours, to enable client-side authenticated messaging. This function should be called from your application's backend after user authentication. ```php "a11aec92-146a-4708-9a62-8c61f46a82ad", "secretKey" => "EIJ2EESAH8DUUMAI8EE3KL5MNP7QRS9T", ]); // Example: Authentication endpoint for your application // This would typically be called from a route handler after user login function authenticatePusherBeams($userId) { global $pushNotifications; try { // Generate token for the authenticated user $beamsToken = $pushNotifications->generateToken($userId); // Return the token to the client return [ "success" => true, "token" => $beamsToken["token"] ]; } catch (Exception $e) { return [ "success" => false, "error" => $e->getMessage() ]; } } // Usage in an API endpoint $userId = "user-0001"; // Get from your authentication system $response = authenticatePusherBeams($userId); header('Content-Type: application/json'); echo json_encode($response); // Output: {"success":true,"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."} ?> ``` -------------------------------- ### Publish Notification to Interests Source: https://context7.com/pusher/push-notifications-php/llms.txt Send a push notification to devices subscribed to specific interests. Supports multiple interests and platform-specific payloads for APNS and FCM. Ensure interests are unique and within the character limit. ```php "a11aec92-146a-4708-9a62-8c61f46a82ad", "secretKey" => "EIJ2EESAH8DUUMAI8EE3KL5MNP7QRS9T", ]); try { // Send notification to multiple interests with platform-specific payloads $publishResponse = $pushNotifications->publishToInterests( ["donuts", "promotions", "news"], // Array of interests (max 100) [ // iOS notification payload (APNS) "apns" => [ "aps" => [ "alert" => [ "title" => "New Promotion!", "body" => "Get 20% off all donuts today!" ], "sound" => "default", "badge" => 1 ] ], // Android notification payload (FCM) "fcm" => [ "notification" => [ "title" => "New Promotion!", "body" => "Get 20% off all donuts today!" ], // Include custom data for the app to process "data" => [ "promo_code" => "DONUT20", "expires_at" => "2024-12-31", "deep_link" => "/promotions/donut20" ] ] ] ); echo "Successfully published with ID: " . $publishResponse->publishId . "\n"; // Output: Successfully published with ID: pub-1234 } catch (Exception $e) { echo "Error publishing notification: " . $e->getMessage() . "\n"; } ``` -------------------------------- ### Publish Notifications to Authenticated Users Source: https://github.com/pusher/push-notifications-php/blob/master/README.md Send secure notifications to individual authenticated users. This method targets specific user IDs with a custom notification payload for APNS and FCM. ```php $publishResponse = $pushNotifications->publishToUsers( ["user-0001"], [ "apns" => [ "aps" => [ "alert" => "Hello!", ], ], "fcm" => [ "notification" => [ "title" => "Hello!", "body" => "Hello, world!", ], ], ] ); echo("Published with Publish ID: " . $publishResponse->publishId . "\n"); ``` -------------------------------- ### Publish Notifications with Data to Device Interests Source: https://github.com/pusher/push-notifications-php/blob/master/README.md Broadcast notifications with custom data to devices subscribed to specific interests. The data payload is included in the FCM notification. ```php $publishResponse = $pushNotifications->publishToInterests( ["donuts"], [ "apns" => [ "aps" => [ "alert" => "Hello!", ], ], "fcm" => [ "notification" => [ "title" => "Hello!", "body" => "Hello, world!", ], "data" => [ "name" => "adam", "type" => "user", ], ], ] ); echo("Published with Publish ID: " . $publishResponse->publishId . "\n"); ``` -------------------------------- ### Publish Notifications to Device Interests Source: https://github.com/pusher/push-notifications-php/blob/master/README.md Broadcast notifications to devices subscribed to specific interests. The notification payload can be customized for APNS and FCM. ```php $publishResponse = $pushNotifications->publishToInterests( ["donuts"], [ "apns" => [ "aps" => [ "alert" => "Hello!", ], ], "fcm" => [ "notification" => [ "title" => "Hello!", "body" => "Hello, world!", ], ], ] ); echo("Published with Publish ID: " . $publishResponse->publishId . "\n"); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.