Add push noti sending
This commit is contained in:
parent
7500f95d88
commit
5b0bea78b7
2 changed files with 41 additions and 26 deletions
|
@ -85,6 +85,7 @@ sub post_login {
|
||||||
return $c->render( json => {
|
return $c->render( json => {
|
||||||
success => Mojo::JSON->true,
|
success => Mojo::JSON->true,
|
||||||
session_key => $session_key,
|
session_key => $session_key,
|
||||||
|
email => $email,
|
||||||
display_name => $user_result->name,
|
display_name => $user_result->name,
|
||||||
user_type => $user_result->type,
|
user_type => $user_result->type,
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,15 +2,18 @@ package Pear::LocalLoop::Controller::Api::Sendmessage;
|
||||||
use Mojo::Base 'Mojolicious::Controller';
|
use Mojo::Base 'Mojolicious::Controller';
|
||||||
use LWP::UserAgent;
|
use LWP::UserAgent;
|
||||||
use JSON;
|
use JSON;
|
||||||
|
use JSON::Parse 'parse_json';
|
||||||
use Mojo::JWT;
|
use Mojo::JWT;
|
||||||
use Mojo::File;
|
use Mojo::File;
|
||||||
use Carp;
|
use Carp;
|
||||||
|
|
||||||
has error_messages => sub {
|
has error_messages => sub {
|
||||||
return {
|
return {
|
||||||
email => {
|
devicetoken => {
|
||||||
required => { message => 'Email is required or not registered', status => 400 },
|
required => { message => 'Device token is required or not registered', status => 400 },
|
||||||
in_resultset => { message => 'Email is required or not registered', status => 400, error => "required" },
|
},
|
||||||
|
sender => {
|
||||||
|
required => { message => 'Sender name is required', status => 400 },
|
||||||
},
|
},
|
||||||
messagetext => {
|
messagetext => {
|
||||||
required => { message => 'Message is required', status => 400 },
|
required => { message => 'Message is required', status => 400 },
|
||||||
|
@ -25,16 +28,19 @@ has error_messages => sub {
|
||||||
https://stackoverflow.com/q/56556438/4580273
|
https://stackoverflow.com/q/56556438/4580273
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
my $jwt = create_jwt_from_path_and_scopes('./localspend-47012.json', 'email https://www.googleapis.com/auth/compute');
|
my $jwt = create_jwt_from_path_and_scopes('./localspend-47012.json', 'email https://www.googleapis.com/auth/cloud-platform');
|
||||||
|
|
||||||
my $ua = LWP::UserAgent->new();
|
my $ua = LWP::UserAgent->new();
|
||||||
|
|
||||||
my $bearer_token = $ua->post('https://www.googleapis.com/oauth2/v4/token',
|
my $response = $ua->post('https://www.googleapis.com/oauth2/v4/token',
|
||||||
{
|
{
|
||||||
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
||||||
'assertion' => $jwt
|
'assertion' => $jwt
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
my $bearer_token = parse_json($response->content);
|
||||||
|
|
||||||
sub create_jwt_from_path_and_scopes
|
sub create_jwt_from_path_and_scopes
|
||||||
{
|
{
|
||||||
my ( $path, $scope ) = @_;
|
my ( $path, $scope ) = @_;
|
||||||
|
@ -66,50 +72,58 @@ sub post_message {
|
||||||
|
|
||||||
my $user_rs = $c->schema->resultset('User');
|
my $user_rs = $c->schema->resultset('User');
|
||||||
|
|
||||||
# $validation->required('email')->in_resultset( 'email', $user_rs );
|
$validation->required('devicetoken');
|
||||||
|
$validation->required('sender');
|
||||||
$validation->required('messagetext');
|
$validation->required('messagetext');
|
||||||
|
|
||||||
return $c->api_validation_error if $validation->has_error;
|
return $c->api_validation_error if $validation->has_error;
|
||||||
|
|
||||||
my $user = $user_rs->find({'email' => $validation->param('email')});
|
|
||||||
|
|
||||||
my $end_point = "https://fcm.googleapis.com/v1/projects/localspend-47012/messages:send";
|
my $end_point = "https://fcm.googleapis.com/v1/projects/localspend-47012/messages:send";
|
||||||
|
|
||||||
my $request = HTTP::Request->new('POST', $end_point);
|
my $request = HTTP::Request->new('POST', $end_point);
|
||||||
$request->header('Authorization' => "Bearer $bearer_token");
|
$request->header('Authorization' => "Bearer $bearer_token->{access_token}");
|
||||||
$request->header('Content-Type' => 'application/json');
|
$request->header('Content-Type' => 'application/json');
|
||||||
|
|
||||||
$request->content(JSON::encode_json ({
|
$request->content(JSON::encode_json ({
|
||||||
message => {
|
message => {
|
||||||
token => $user->param('token'),
|
token => $validation->param('devicetoken'),
|
||||||
notification => {
|
notification => {
|
||||||
title => 'test',
|
title => $validation->param('sender'),
|
||||||
body => 'test content'
|
body => $validation->param('messagetext')
|
||||||
},
|
},
|
||||||
webpush => {
|
webpush => {
|
||||||
headers => {
|
headers => {
|
||||||
Urgency => 'high'
|
urgency => 'very-low'
|
||||||
},
|
},
|
||||||
notification => {
|
notification => {
|
||||||
body => 'test content',
|
title => $validation->param('sender'),
|
||||||
requireInteraction => 'true'
|
body => $validation->param('messagetext'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
$ua->request($request);
|
my $response = $ua->request($request);
|
||||||
|
|
||||||
=begin comment
|
if ($response->is_success) {
|
||||||
$c->schema->resultset('Feedback')->create({
|
return $c->render( json => {
|
||||||
user => $user,
|
success => Mojo::JSON->true,
|
||||||
messagetext => $validation->param('messagetext'),
|
message => 'Your message has been sent successfully!',
|
||||||
});
|
});
|
||||||
=cut
|
} elsif ($response->is_error) {
|
||||||
return $c->render( json => {
|
return $c->render(
|
||||||
success => Mojo::JSON->true,
|
json => {
|
||||||
message => 'Your message has been sent successfully!',
|
success => Mojo::JSON->false,
|
||||||
});
|
message => [
|
||||||
|
$response->decoded_content,
|
||||||
|
$jwt,
|
||||||
|
$bearer_token
|
||||||
|
],
|
||||||
|
error => 'message_error',
|
||||||
|
},
|
||||||
|
status => $response->code,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
Reference in a new issue