-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
187 additions
and
13 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
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
66 changes: 66 additions & 0 deletions
66
app/Http/Controllers/User/Notification/NotificationSendController.php
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\User\Notification; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use App\Models\User; | ||
use Auth; | ||
|
||
class NotificationSendController extends Controller | ||
{ | ||
public function updateDeviceToken(Request $request) | ||
{ | ||
Auth::user()->device_token = $request->token; | ||
|
||
Auth::user()->save(); | ||
|
||
return response()->json([ | ||
'status' => true, | ||
]); | ||
} | ||
|
||
public function sendNotification(Request $request) | ||
{ | ||
$url = 'https://fcm.googleapis.com/fcm/send'; | ||
|
||
$FcmToken = User::whereNotNull('device_token')->pluck('device_token')->all(); | ||
|
||
$serverKey = 'AAAANPt3SJI:APA91bF_gxrIoSlxH6j544E_ZeX71fGd4qgyKuyXN72l44ok_FdxDU32gMK6vLMZoMs0W0cufeS_gJ6O1JwaF5X-aRx8l7fPbIs5KfphbSX6x7-VK-d4wjG8qQ_q3hnh-2eSjLhHI0cu'; // Add Server Key Here Provided By FCM | ||
|
||
$data = [ | ||
"registration_ids" => $FcmToken, | ||
"notification" => [ | ||
"title" => $request->title, | ||
"body" => $request->body, | ||
] | ||
]; | ||
$encodedData = json_encode($data); | ||
|
||
$headers = [ | ||
'Authorization:key=' . $serverKey, | ||
'Content-Type: application/json', | ||
]; | ||
|
||
$ch = curl_init(); | ||
|
||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_POST, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | ||
curl_setopt($ch, CURLOPT_HTTP_VERSION, true); | ||
// Disabling SSL: Certificate support temporarly | ||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData); | ||
// Execute post | ||
$result = curl_exec($ch); | ||
if($result === FALSE){ | ||
die('Curl failed: ' . curl_error($ch)); | ||
} | ||
// Close connection | ||
curl_close($ch); | ||
// FCM response | ||
dd($result); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Give the service worker access to Firebase Messaging. | ||
// Note that you can only use Firebase Messaging here. Other Firebase libraries | ||
// are not available in the service worker.importScripts('https://www.gstatic.com/firebasejs/7.23.0/firebase-app.js'); | ||
importScripts('https://www.gstatic.com/firebasejs/8.3.2/firebase-app.js'); | ||
importScripts('https://www.gstatic.com/firebasejs/8.3.2/firebase-messaging.js'); | ||
/* | ||
Initialize the Firebase app in the service worker by passing in the messagingSenderId. | ||
*/ | ||
firebase.initializeApp({ | ||
apiKey: "AIzaSyCb4-gYuRCchkACI-z-H0N3BGZSVE1OPTw", | ||
authDomain: "magnetic-lore-322008.firebaseapp.com", | ||
projectId: "magnetic-lore-322008", | ||
storageBucket: "magnetic-lore-322008.appspot.com", | ||
messagingSenderId: "227557197970", | ||
appId: "1:227557197970:web:aad3453997eb67a7073e4b" | ||
}); | ||
|
||
// Retrieve an instance of Firebase Messaging so that it can handle background | ||
// messages. | ||
const messaging = firebase.messaging(); | ||
messaging.setBackgroundMessageHandler(function(payload) { | ||
console.log("Message received.", payload); | ||
const title = "Hello world is awesome"; | ||
const options = { | ||
body: "Your notificaiton message .", | ||
icon: "/firebase-logo.png", | ||
}; | ||
return self.registration.showNotification( | ||
title, | ||
options, | ||
); | ||
}); |
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,23 +1,86 @@ | ||
@extends('layouts.app') | ||
|
||
@section('content') | ||
<div class="container"> | ||
<div class="row justify-content-center"> | ||
<div class="col-md-8"> | ||
<div class="card"> | ||
<div class="card-header">{{ __('Dashboard') }}</div> | ||
|
||
<button onclick="startFCM()" | ||
class="btn btn-danger btn-flat">Allow notification | ||
</button> | ||
<div class="card mt-3"> | ||
<div class="card-body"> | ||
@if (session('status')) | ||
<div class="alert alert-success" role="alert"> | ||
{{ session('status') }} | ||
</div> | ||
<div class="alert alert-success" role="alert"> | ||
{{ session('status') }} | ||
</div> | ||
@endif | ||
|
||
{{ __('You are logged in!') }} | ||
<form action="{{ route('send.web-notification', app()->getlocale() ) }}" method="POST"> | ||
@csrf | ||
<div class="form-group"> | ||
<label>Message Title</label> | ||
<input type="text" class="form-control" name="title"> | ||
</div> | ||
<div class="form-group"> | ||
<label>Message Body</label> | ||
<textarea class="form-control" name="body"></textarea> | ||
</div> | ||
<button type="submit" class="btn btn-success btn-block">Send Notification</button> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
@endsection | ||
<!-- The core Firebase JS SDK is always required and must be listed first --> | ||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | ||
<script src="https://www.gstatic.com/firebasejs/8.3.2/firebase.js"></script> | ||
<script> | ||
var firebaseConfig = { | ||
apiKey: "AIzaSyCb4-gYuRCchkACI-z-H0N3BGZSVE1OPTw", | ||
authDomain: "magnetic-lore-322008.firebaseapp.com", | ||
projectId: "magnetic-lore-322008", | ||
storageBucket: "magnetic-lore-322008.appspot.com", | ||
messagingSenderId: "227557197970", | ||
appId: "1:227557197970:web:aad3453997eb67a7073e4b" | ||
}; | ||
firebase.initializeApp(firebaseConfig); | ||
const messaging = firebase.messaging(); | ||
function startFCM() { | ||
messaging | ||
.requestPermission() | ||
.then(function () { | ||
return messaging.getToken() | ||
}) | ||
.then(function (response) { | ||
$.ajaxSetup({ | ||
headers: { | ||
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') | ||
} | ||
}); | ||
$.ajax({ | ||
url: '{{ route("store.token", app()->getlocale()) }}', | ||
type: 'POST', | ||
data: { | ||
token: response | ||
}, | ||
dataType: 'JSON', | ||
success: function (response) { | ||
alert('Token stored.'); | ||
}, | ||
error: function (error) { | ||
alert(error); | ||
}, | ||
}); | ||
}).catch(function (error) { | ||
alert(error); | ||
}); | ||
} | ||
messaging.onMessage(function (payload) { | ||
const title = payload.notification.title; | ||
const options = { | ||
body: payload.notification.body, | ||
icon: payload.notification.icon, | ||
}; | ||
new Notification(title, options); | ||
}); | ||
</script> | ||
@endsection |
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