forked from panicbit/fcm-rust
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jutuon/major-update
Major update
- Loading branch information
Showing
31 changed files
with
792 additions
and
1,116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
# follow the instructions in the [Firebase Documentation](https://firebase.google.com/docs/cloud-messaging/auth-server#provide-credentials-manually) to create a service account. after you create a service account, and download the json file then change the value of `GOOGLE_APPLICATION_CREDENTIALS` to the path of the json file you downloaded. | ||
# Follow the instructions in the | ||
# [Firebase Documentation](https://firebase.google.com/docs/cloud-messaging/auth-server#provide-credentials-manually) | ||
# to download service account key JSON file. After downloading | ||
# the JSON file then change the value of `GOOGLE_APPLICATION_CREDENTIALS` to | ||
# the path of the JSON file you downloaded. | ||
GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/firebase/file.json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,54 @@ | ||
// cargo run --example simple_sender -- -t <device_token> | ||
// cargo run --example simple_sender -- --help | ||
|
||
use argparse::{ArgumentParser, Store}; | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
use fcm::{ | ||
AndroidConfig, AndroidNotification, ApnsConfig, Client, FcmOptions, Message, Notification, Target, WebpushConfig, | ||
message::{Message, Notification, Target}, | ||
FcmClient, | ||
}; | ||
use serde_json::json; | ||
|
||
#[derive(Parser, Debug)] | ||
struct CliArgs { | ||
#[arg(short = 't', long)] | ||
device_token: String, | ||
/// Set path to the service account key JSON file. Default is to use | ||
/// path from the `GOOGLE_APPLICATION_CREDENTIALS` environment variable | ||
/// (which can be also located in `.env` file). | ||
#[arg(short = 'k', long, value_name = "FILE")] | ||
service_account_key_path: Option<PathBuf>, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
pretty_env_logger::init(); | ||
|
||
let mut device_token = String::new(); | ||
|
||
{ | ||
let mut ap = ArgumentParser::new(); | ||
ap.set_description("A simple FCM notification sender"); | ||
ap.refer(&mut device_token) | ||
.add_option(&["-t", "--device_token"], Store, "Device token"); | ||
ap.parse_args_or_exit(); | ||
} | ||
|
||
let client = Client::new(); | ||
let args = CliArgs::parse(); | ||
let builder = FcmClient::builder(); | ||
let builder = if let Some(path) = args.service_account_key_path { | ||
builder.service_account_key_json_path(path) | ||
} else { | ||
builder | ||
}; | ||
|
||
let data = json!({ | ||
"key": "value", | ||
}); | ||
let client = builder.build().await.unwrap(); | ||
|
||
let builder = Message { | ||
data: Some(data), | ||
let message = Message { | ||
data: Some(json!({ | ||
"key": "value", | ||
})), | ||
notification: Some(Notification { | ||
title: Some("I'm high".to_string()), | ||
body: Some(format!("it's {}", chrono::Utc::now())), | ||
..Default::default() | ||
}), | ||
target: Target::Token(device_token), | ||
fcm_options: Some(FcmOptions { | ||
analytics_label: "analytics_label".to_string(), | ||
}), | ||
android: Some(AndroidConfig { | ||
priority: Some(fcm::AndroidMessagePriority::High), | ||
notification: Some(AndroidNotification { | ||
title: Some("I'm Android high".to_string()), | ||
body: Some(format!("Hi Android, it's {}", chrono::Utc::now())), | ||
..Default::default() | ||
}), | ||
title: Some("Title".to_string()), | ||
..Default::default() | ||
}), | ||
apns: Some(ApnsConfig { ..Default::default() }), | ||
webpush: Some(WebpushConfig { ..Default::default() }), | ||
target: Target::Token(args.device_token), | ||
fcm_options: None, | ||
android: None, | ||
apns: None, | ||
webpush: None, | ||
}; | ||
|
||
let response = client.send(builder).await?; | ||
println!("Sent: {:?}", response); | ||
let response = client.send(message).await?; | ||
println!("Response: {:#?}", response); | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.