https://github.com/ftadev/fcm-example
https://github.com/ftadev/fcm-example
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ftadev/fcm-example
- Owner: FtADev
- Created: 2023-10-31T12:16:10.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-05T09:27:46.000Z (over 2 years ago)
- Last Synced: 2025-02-01T21:28:19.122Z (over 1 year ago)
- Language: C++
- Size: 616 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Firebase Cloud Messaging Example
## Getting Started
Create a [firebase](https://console.firebase.google.com/project/) project.
add name and continue
then click on Android

Go to /android/app/src/main/AndroidManifest.xml
copy "package" name (if does not have, add it at the end of xmlns: `package="com.example.fcm_example"`)
add it in firebase console

and click on Register app.
download `google-services.json` and put it under /src folder and add next.
To make the google-services.json config values accessible to Firebase SDKs, you need the Google services Gradle plugin.
Add the plugin as a dependency to your project-level build.gradle.kts file:
Root-level (project-level) Gradle file (/build.gradle):
```
dependencies {
...
classpath 'com.google.gms:google-services:4.4.0'
}
```
Then, in module (app-level) Gradle file (//build.gradle):
```
plugins {
// Add the Google services Gradle plugin
id "com.google.gms.google-services"
}
dependencies {
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:32.4.1')
// TODO: Add the dependencies for Firebase products you want to use
// When using the BoM, don't specify versions in Firebase dependencies
//implementation("com.google.firebase:firebase-analytics-ktx")
// Add the dependencies for any other desired Firebase products
// https://firebase.google.com/docs/android/setup#available-libraries
}
```
After adding the plugin and the desired SDKs, sync your Android project with Gradle files.
## Send Background Notification
After adding `firebase_api` and changing `main` method in [first commit](https://github.com/FtADev/FCM-Example/commit/a7e8b03663afd963c8756678d9266b1414126f83) and [second commit](https://github.com/FtADev/FCM-Example/commit/4979ec2c8b77e8bf328e074421b6bb11d1dbf157)
you can push a notification from your firebase console
Go to your project page



After filling the fields click on send test message

Here you can add your token (That we were printing on the code) to target that specific device and click on add icon.
put the app in the background (or terminate it) and click on test!
## Routing
If you want to route to a specific screen by clicking on notification, you can see these commits:
1. [Add screens](https://github.com/FtADev/FCM-Example/commit/15f934c6974b19840c3202416d31eb6faa991ae8)
2. [Handle Messages](https://github.com/FtADev/FCM-Example/commit/8914a8cdc8275cca64b35324bed14537b2045957)
## Local Notifications
What should we do if we want to receive a notification when app is in foreground?
Just follow [this commit](https://github.com/FtADev/FCM-Example/commit/23bc2f58b405085def4785dc0e44e25c9bef9015)!
Thanks to [HeyFlutter](https://www.youtube.com/watch?v=k0zGEbiDJcQ)
## Postman
If you want to send notification from postman:
Go to your project in firebase console:
Project Overview->Project Settings->Cloud messaging-> Enable `Cloud Messaging API (Legacy)`

then copy the `Server Key`
In Postman:
Create a new request:
URL:
```
https://fcm.googleapis.com/fcm/send
```
Method:
```
POST
```
Header:
```
"Content-Type": "application/json",
"Authorization": "key="
```
Body:
```
{
"to": "",
"notification": {
"title": "Push Notification",
"body": "Test Push Notification",
"something": "somewhere"
},
"data": {
"title": "data title",
"message": "data message",
"something": "anotherthing"
}
}
```
(`data` part is payload)