Skip to content

Instantly share code, notes, and snippets.

@pythoneer
Created June 18, 2018 15:12
Show Gist options
  • Save pythoneer/aa61dffc39debc86044b6849e14a3ce7 to your computer and use it in GitHub Desktop.
Save pythoneer/aa61dffc39debc86044b6849e14a3ce7 to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate serde_derive;
extern crate actix_web;
extern crate fcm;
extern crate tokio;
extern crate futures;
use actix_web::{server, App, Responder, Json, http, HttpResponse, Error, client::SendRequestError};
use fcm::{MessageBuilder, Client, NotificationBuilder, FcmError};
use futures::{
future::lazy,
Future,
};
#[derive(Deserialize)]
struct FcmRequest {
device_token: String,
api_key: String,
message: String,
}
#[derive(Serialize)]
struct CustomData {
message: String,
click_action: String,
}
fn push_fcm(fcm_req: Json<FcmRequest>) -> impl Future<Item = HttpResponse, Error = SendRequestError> {
let client = Client::new().unwrap();
let data = CustomData {
message: fcm_req.message.clone(),
click_action: "FLUTTER_NOTIFICATION_CLICK".into(),
};
let msg = format!("this is the future: {}", fcm_req.message);
let notification = {
let mut notification_builder = NotificationBuilder::new();
notification_builder.title("title");
notification_builder.body(&msg);
notification_builder.finalize()
};
let payload = {
let mut builder = MessageBuilder::new(&fcm_req.api_key, &fcm_req.device_token);
builder.data(&data).unwrap();
builder.notification(notification);
builder.finalize()
};
let sending = client.send(payload);
let http_resp = sending.map(|response| {
println!("Sent: {:?}", response);
HttpResponse::Ok()
.content_type("plain/text")
.header("X-Hdr", "sample")
.body("data>")
}).map_err(|error| {
println!("Error: {:?}", error);
SendRequestError::Timeout
});
http_resp
}
fn main() {
println!("starting server..");
server::new(|| {
App::new().resource("/push_fcm", |r| r.method(http::Method::POST).with_async(push_fcm))
})
.bind("0.0.0.0:8080")
.unwrap()
.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment