1// Install this package
2composer require laravel-notification-channels/fcm:~2.0
3
4// Create a notification for user and add the following code
5class AccountActivated extends Notification
6{
7 public function via($notifiable)
8 {
9 return [FcmChannel::class];
10 }
11
12 public function toFcm($notifiable)
13 {
14 return FcmMessage::create()
15 ->setData(['data1' => 'value', 'data2' => 'value2'])
16 ->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
17 ->setTitle('Account Activated')
18 ->setBody('Your account has been activated.')
19 ->setImage('http://example.com/url-to-image-here.png'))
20 ->setAndroid(
21 AndroidConfig::create()
22 ->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
23 ->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
24 )->setApns(
25 ApnsConfig::create()
26 ->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
27 }
28}
29
30// Add Notifiable trait to user class
31// Add Method routeNotificationForFcm to return save token from firebase cloud messaging in database
32
33class User extends Authenticatable
34{
35 use Notifiable;
36
37 /**
38 * Specifies the user's FCM token
39 *
40 * @return string
41 */
42 public function routeNotificationForFcm()
43 {
44 return $this->fcm_token;
45 }
46}
47
48
49// Fire the notification to test it
50$user->notify(new AccountActivated);