Skip to content

Instantly share code, notes, and snippets.

@mrron313
Last active September 8, 2020 05:01
Show Gist options
  • Save mrron313/1d0972ad4f7ff418656b8a28790358c2 to your computer and use it in GitHub Desktop.
Save mrron313/1d0972ad4f7ff418656b8a28790358c2 to your computer and use it in GitHub Desktop.
home.blade.php
@extends('layouts.app')
@section('content')
<div id="app" class="container">
<div class="row justify-content-center">
<div class="col-md-8">
@if(Session::has('message'))
<div class="alert alert-success">
{{session('message')}}
</div>
@endif
<div class="card">
@if ($user_id == 1)
<div class="card-header">Send push to Users</div>
<div class="card-body">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>
<form action="{{ route('send-push') }}" method="post">
@csrf
<input type="hidden" name="id" value="{{$user->id}}" />
<input class="btn btn-primary" type="submit" value="Send Push">
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@else
<div class="card-header">User Panel</div>
@endif
</div>
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="https://www.gstatic.com/firebasejs/6.3.4/firebase.js"></script>
<script>
$(document).ready(function(){
const config = {
apiKey: "<XXX>",
authDomain: "<XXX>",
databaseURL: "<XXX>",
projectId: "<XXX>",
storageBucket: "<XXX>",
messagingSenderId: "<XXX>",
appId: "<XXX>"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging
.requestPermission()
.then(function () {
return messaging.getToken()
})
.then(function(token) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: '{{ URL::to('/save-device-token') }}',
type: 'POST',
data: {
user_id: {!! json_encode($user_id ?? '') !!},
fcm_token: token
},
dataType: 'JSON',
success: function (response) {
console.log(response)
},
error: function (err) {
console.log(" Can't do because: " + err);
},
});
})
.catch(function (err) {
console.log("Unable to get permission to notify.", err);
});
messaging.onMessage(function(payload) {
const noteTitle = payload.notification.title;
const noteOptions = {
body: payload.notification.body,
icon: payload.notification.icon,
};
new Notification(noteTitle, noteOptions);
});
});
</script>
@endsection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment