mercredi 3 février 2021

FCM Notification onMessage() is working but it is only showing data in console. There is no popup on chrome

messaging.onMessage(function (payload) is showing data in console but there is no popup notification in chrome.

<script src="https://www.gstatic.com/firebasejs/7.8.1/firebase.js"></script>
<script>

   messaging.onMessage(function (payload) {
    try{
    console.log('onMessage. ', payload);
    const title = payload.notification.title;

    const options = {
        body: payload.notification.body,
    };
    console.log("title ",title, " ", payload.notification.body);
    new Notification(title, options);
    }
    catch(err){
        console.log('Caught error: ',err);
    }

});

console.log("title ",title, " ", payload.notification.body); is also showing me the values in console but throwing no notification.

firebase-messaging-sw.js

const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {

   var title = payload.data.title;

   var options = {
       body: payload.data.body,
   };

   return self.registration.showNotification(title, options);

});

Incase if my controller has something to do with it.

$firebaseToken = DB::table('users')->where('id' , '2')->value('token');
    $SERVER_API_KEY = 'AAAAmRamdCg:APA91bGQKgtq6LuIKIo9M0aWjW66BM0ltPONB0UUvpsgP79Bfi_Kw98oV8UFIkSK0OUbv8_HehnsHO3jez3V83zbErbsGa8WYekJFbE4YGzbHuyQRL8TPPmVfVnZyCJUhMY1bgjMaXud';

    $data = [
        "to" => $firebaseToken,
        "notification" => [
            "title" => 'Hi',
            "body" => 'Hello',
        ]
    ];

    $dataString = json_encode($data);

    $headers = [
        'Authorization: key=' . $SERVER_API_KEY,
        'Content-Type: application/json',
    ];

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);

    $response = curl_exec($ch);

    dd($response);

What am i missing or doing wrong here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire