How can I send a Firebase Cloud Messaging notification without use the Firebase Console?

后端 未结 16 1290
南旧
南旧 2020-11-22 10:17

I\'m starting with the new Google service for the notifications, Firebase Cloud Messaging.

Thanks to this code https://github.com/firebase/quickstart-a

16条回答
  •  孤街浪徒
    2020-11-22 10:41

    If you're using PHP, I recommend using the PHP SDK for Firebase: Firebase Admin SDK. For an easy configuration you can follow these steps:

    Get the project credentials json file from Firebase (Initialize the sdk) and include it in your project.

    Install the SDK in your project. I use composer:

    composer require kreait/firebase-php ^4.35
    

    Try any example from the Cloud Messaging session in the SDK documentation:

    use Kreait\Firebase;
    use Kreait\Firebase\Messaging\CloudMessage;
    
    $messaging = (new Firebase\Factory())
    ->withServiceAccount('/path/to/firebase_credentials.json')
    ->createMessaging();
    
    $message = CloudMessage::withTarget(/* see sections below */)
        ->withNotification(Notification::create('Title', 'Body'))
        ->withData(['key' => 'value']);
    
    $messaging->send($message);
    

提交回复
热议问题