2

Laravel FCM push notification not working for iOS, but working with Android - brozot/laravel-fcm

config(['fcm.http.server_key' => $shop_data->fcm_server_key]);
                config(['fcm.http.sender_id' => $shop_data->fcm_sender_id]);

                $optionBuilder = new OptionsBuilder();
                $optionBuilder->setTimeToLive(60 * 20);

                $notificationBuilder = new PayloadNotificationBuilder($message);
                $notificationBuilder->setBody($message_image)
                    ->setSound('default');

                $dataBuilder = new PayloadDataBuilder();
                $dataBuilder->addData(['message' => trim($message), 'image' => trim($message_image)]);

                $option = $optionBuilder->build();
                $notification = $notificationBuilder->build();
                $data = $dataBuilder->build();

                $device_groups = DeviceGroup::where('shop_id', $shop_id)->get();
                foreach ($device_groups as $device_group) {
                    $groupResponse = FCM::sendToGroup($device_group->group_key, $option, null, $data);
                    $groupResponse->numberSuccess();
                    $groupResponse->numberFailure();
                    $groupResponse->tokensFailed();
                }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Keyur G
  • 21
  • 4

3 Answers3

0

This is for FCM for iOS. Its a good idea to put it in a Trait. Don't forget to include:

use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
use FCM;
use Log;

  try {

            $sound = 'default';
            if (!empty($notification_sound)) {
                $sound = $notification_sound;
            }

            $ttl = 15;
            if (!empty($time_to_live)) {
                $ttl = $time_to_live;
            }


            $notificationBuilder = new PayloadNotificationBuilder();
            $notificationBuilder
                ->setTitle($push_tittle)
                ->setSound($sound)
                //                ->setIcon(FAV_ICON)
                //                ->setColor('#fc547f')
            ;

            $dataBuilder = new PayloadDataBuilder();
            $dataBuilder->addData([
                'custom' => $data //sending custom data
            ]);

            $optionBuilder = new OptionsBuilder();
            $optionBuilder->setTimeToLive($ttl);


            $notification = $notificationBuilder->build();
            $data = $dataBuilder->build();
            $option = $optionBuilder->build();



            //            Log::debug( ' Push $notification' .  json_encode($notification->toArray()));
            //            Log::debug( ' Push $option' .  json_encode($option->toArray()));
            //            Log::debug( '  Push $data' .  json_encode($data->toArray()));

            $downstreamResponse = FCM::sendTo($device_id, $option, $notification, $data);





            return $downstreamResponse;

        } catch (\Exception $e) {

            Log::debug(' Error message Push  ' . $e->getMessage());
            Log::debug(' Error message Push  ' . $e->getFile());
            Log::debug(' Error message Push  ' . $e->getLine());
            return $e->getMessage();
        }
Iliyan
  • 61
  • 1
  • 4
0

If you are trying using ios simulator it will not work. You need to use some tools like testflight or real device to test.

can we check push notification in simulator?

Harish Krishnan
  • 1,693
  • 4
  • 17
  • 25
0

You have to use setContentAvailable(true) to send silent notification to iOS:

$optionBuilder = new OptionsBuilder(); 
$optionBuilder->setContentAvailable(true); 
$optionBuilder->setPriority(OptionsPriorities::normal); 
$dataBuilder = new PayloadDataBuilder(); 
$dataBuilder->addData($data); 
$option = $optionBuilder->build(); 
$data = $dataBuilder->build();

$downstreamResponse = FCM::sendTo($token, $option, null, $data);
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68