Skip to content

Push notification using Firebase Cloud Messaging with php and javascript

Notifications You must be signed in to change notification settings

christiansmith812/firebase-cloud-messaging-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Firebase Cloud Messaging: Sending Push Notifications with php and javascript

1. Creating a Firebase Project in the Console

Url: https://console.firebase.google.com/


2. Registering the App With Firebase

Press "Add app" button (android, ios, webapp)


3. Project settings

  • Firebase config for clients: Project settings > General > Your apps
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.15.0/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.15.0/firebase-analytics.js"></script>

<script>

  // Your web app's Firebase configuration
  var firebaseConfig = {
    apiKey: "<<FCM_API_KEY>>",
    authDomain: "<<FCM_AUTH_DOMAIN>>.firebaseapp.com",
    databaseURL: "https://<<FCM_DATABASE_URL>>.firebaseio.com",
    projectId: "<<FCM_PROJECT_ID>>",
    storageBucket: "<<FCM_STORAGE_BUCKET>>.appspot.com",
    messagingSenderId: "<<FCM_MESSAGING_SENDER_ID>>",
    appId: "<<FCM_APP_ID>>",
    measurementId: "<<FCM_MEASUREMENT_ID>>"
  };

  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
</script>
  • Server key to the server side
 Key	      Token	
 Server key   <<FCM_SERVER_KEY>>

4. Create topic for notifications

"Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices."

Steps:


5. Getting Started

  • Client:
    • copy firebase-messaging-sw.js in the root of your domain
    • change the config (<<FCM_API_KEY>>, <<FCM_AUTH_DOMAIN>>, etc.) in firebase-messaging-sw.js and client.html files
  • Server:
    • change the <<FCM_SERVER_KEY>> value in the server.php file

6. Server Reference

Server implementation is optional. Use the Instance ID service if you want to perform these operations:

  • Get information about app instances. Verify app tokens or get more information about the app instance that created the token.
  • Create relationship maps for app instances. Create relationships between app instances and entities such as FCM or GCM topics.
  • Create registration tokens for APNs tokens. This API lets you bulk import existing APNs tokens, mapping them to valid registration tokens for FCM or GCM.
  • Manage registration tokens for push subscriptions. For web applications implemented using the Push API, import your existing push subscriptions, mapping them to valid registration tokens for FCM.
  • Get information about app instances

    • Request: GET
    • Url: https://iid.googleapis.com/iid/info/<<IID_TOKEN>>
    • Headers:
      • Key: Authorization
      • Value: key=<<FCM_SERVER_KEY>>
  • Create a relation mapping for an app instance

    • Request: POST
    • Url: https://iid.googleapis.com/iid/v1/<<IID_TOKEN>>/rel/topics/<<TOPIC_NAME>>
    • Headers:
      • Key: Authorization
      • Value: key=<<FCM_SERVER_KEY>>

7. Useful pages

About

Push notification using Firebase Cloud Messaging with php and javascript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages