May 4, 2019
0
Level 2
help with Firebase php Notification;
hi, i'm trying to send / recive notification using firebase
i know how to upload cloude messging but how i can recive the notificaion after that ?
firebase controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Kreait\Firebase;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use Kreait\Firebase\Database;
use Kreait\Firebase\Messaging\Message;
use Kreait\Firebase\Messaging\MessageToTopic;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\CloudMessage;
class FirebaseController extends Controller
{
public function index(){
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'asdasd-firebase-adminsdk-6rxwm-58430d1f78.json');
$firebase = (new Factory)
->withServiceAccount($serviceAccount)
->withDatabaseUri('https://asdasd-e12f2.firebaseio.com')
->create();
$database = $firebase->getDatabase();
$newPost = $database
->getReference('blog/posts')
->push(['title' => 'Post title','body' => 'This should probably be longer.']);
echo"<pre>";
print_r($newPost->getvalue());
$topic = 'a-topic';
$message = CloudMessage::withTarget('topic', $topic);
$message = CloudMessage::fromArray([
'topic' => $topic
]);
$registrationTokens = [
$topic
];
$messaging = $firebase->getMessaging();
$messaging->subscribeToTopic($topic, $registrationTokens);
$messaging->send($message);
$title = 'My Notification Title';
$body = 'My Notification Body';
$notification = Notification::fromArray([
'title' => $title,
'body' => $body
]);
$notification = Notification::create($title, $body);
$data = [
'first_key' => 'First Value',
'second_key' => 'Second Value',
];
$message = $message->withNotification($notification);
$message = $message->withData($data);
}
}
Please or to participate in this conversation.