PHP code example of irfanmumtaz / firebase-cloud-message

1. Go to this page and download the library: Download irfanmumtaz/firebase-cloud-message 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/ */

    

irfanmumtaz / firebase-cloud-message example snippets


{
    "  "irfanmumtaz/firebase-cloud-message": "^0.1.1"
    }
}

'providers' => [
	// ...

	Irfanmumtaz\FirebaseCloudMessage\FCMServiceProvider::class,
]

use Irfanmumtaz\FirebaseCloudMessage\FirebaseCM;
use Irfanmumtaz\FirebaseCloudMessage\NotificationBuilder;

//create notification builder
$notification = new NotificationBuilder("Test notification");
$notification->setBody("test")
        ->tag('larvael');

/**
 * you can set other notification params 
 * documentation here https://firebase.google.com/docs/cloud-messaging/http-server-ref
 * setBody(), setSound(), setBadge(), setClickAction(), setSubTitle(), setBodyLocKey(),
 * setBodyLocArgs(), setTitleLocKey(), setTitleLocArgs(), setAndroidChannelId(), setIcon(), setTag(),
 * setColor()
 */

//you can set option parameters
$notification->options->setCollapseKey("example")->setPriority("high");

/**
 * you can set other option params
 * documentation here https://firebase.google.com/docs/cloud-messaging/http-server-ref
 * setCollapseKey(), setPriority(), setContentAvailable(), setMutableContent(),
 * setTimeToLive(), setRestrictedPackageName(), setDryRun()
 * 
 */

//custom data can be added in this way
$notification->custom->addData("key1", "value1")->addData("key2", "value2");

/**
 * Other usable functions in custom data
 * addData("key", "value") you can add custom data as many you want
 * removeData() remove all keys from custom data
 * unsetData("key1") remove a single key from custom data
 * getData() get complete custom data as array
 */


//create an object for FCM and pass notification while creating object
$fcm = new FirebaseCM($notification);
$fcm->setTo('firebase key')->send();
/**
 * Other FCM functions 
 * setTo("key") uses for sending notification to a single user, pass token as string
 * setRegistrations(["key1", "key2"]) uses for sending notification to multiple users pass array of string
 * send() uses to send notificaiton
 */