laravel-notification-channels / fcm

Firebase Cloud Messaging (FCM) notifications channel for Laravel
https://laravel-notification-channels.com/
MIT License
493 stars 127 forks source link

Send fcmtoken successfully, but the notification on the mobile doesn't appear #209

Closed syahroel712 closed 2 weeks ago

syahroel712 commented 3 weeks ago

hello sir, I have a problem suddenly my fcmtoken is not working anymore, I use version 2.7.0 in laravel 8.83 with php version 7.4.

please help how to fix it because when I try to debug and see in laravel.log then the result is successfully sent, but unfortunately the notification does not appear on the mobile.

here is my code:

in controller: `public function testKirimNotifikasi(Request $request) { $pesan = $request->input('pesan', 'Pesan default untuk notifikasi.'); $user_id = $request->input('user_id');

    if (!$user_id) {
        return response()->json(['success' => false, 'message' => 'User ID diperlukan untuk pengujian.'], 400);
    }

    try {
        // Panggil function kirimNotifikasi
        $this->kirimNotifikasi($pesan, $user_id);

        return response()->json(['success' => true, 'message' => 'Notifikasi berhasil dikirim.']);
    } catch (\Exception $e) {
        return response()->json(['success' => false, 'message' => 'Terjadi kesalahan saat mengirim notifikasi: ' . $e->getMessage()], 500);
    }
}

public function kirimNotifikasi($pesan, $user_id)
{
    $user = User::find($user_id);
    if ($user->web_token != null || $user->fcm_token != null) {
        $chanels = [FcmChannel::class];
        $user->notify(new \App\Notifications\FirebaseNotification($chanels, $pesan));
    }
}`

in notification: `<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; use NotificationChannels\Fcm\FcmMessage;

class FirebaseNotification extends Notification { use Queueable;

/**
 * Create a new notification instance.
 *
 * @return void
 */

private $channels;
private $pesan;
public function __construct(array $channels, String $pesan)
{
    $this->channels = $channels;
    $this->pesan = $pesan;
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return $this->channels;
}

public function toFcm($notifiable)
{
    $message = FcmMessage::create()
          ->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
              ->setTitle('Title')
              ->setBody($this->pesan));
      \Log::info('FCM Message Created: ', ['message' => json_encode($message)]);
      return $message;
}

}`

in laravel.log: local.INFO: FCM Message Created: {"message":"{\"name\":null,\"data\":null,\"notification\":{\"title\":\"Title\",\"body\":\"Pesan default untuk notifikasi.\",\"image\":null},\"android\":null,\"webpush\":null,\"apns\":null,\"fcm_options\":null}"}

in my respon: {"success":true,"message":"Notifikasi berhasil dikirim."}

msl2000 commented 1 week ago

Hi. What was the solution to your problem? Ran into the same issue.

syahroel712 commented 1 week ago

Hi. What was the solution to your problem? Ran into the same issue.

hi, for this problem I tried another way, I didn't use fcm anymore, I changed to kreait/firebase-php. here are the settings in my composer. "require": { "firebase/php-jwt": "^6.3.2", "creait/firebase-php": "^7.0" }

I hope this can help you