Sh3ll
OdayForums


Server : Apache
System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64
User : tradze ( 1001)
PHP Version : 8.1.34
Disable Function : NONE
Directory :  /home/tradze/.trash/app_old/Modules/Notifications/Repositories/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/tradze/.trash/app_old/Modules/Notifications/Repositories/31-March-2020-NotifRepository.php
<?php
namespace App\Modules\Notifications\Repositories;

use App\Modules\Users\Models\UserDevice;
use App\Modules\Notifications\Models\Notification;
use Exception;

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Sly\NotificationPusher\PushManager,
    Sly\NotificationPusher\Adapter\Apns as ApnsAdapter,
    Sly\NotificationPusher\Adapter\Gcm as GcmAdapter,
    Sly\NotificationPusher\Collection\DeviceCollection,
    Sly\NotificationPusher\Model\Device,
    Sly\NotificationPusher\Model\Message,
    Sly\NotificationPusher\Model\Push
    ;
use Illuminate\Support\Facades\Mail;

class NotifRepository
{


    /**
     * Register new notification
     * @param array $data
     */
    public function add($user_id,$message)
    {

        //get all user devices
        $devices = UserDevice::where('user_id',$user_id)
                            ->whereNotNull('push_token')
                            ->where('push_token','<>','null')
                            ->get();

        //stop the script if no devices is attached to the user account
        if (!$devices->count())
            return;

        //foreach user device, add the notification
        foreach($devices as $device){

            //if the registered device push_token is null, continue
            if (!isset($device->push_token))
                continue;

            //create push notification data array
            $data = [
                'user_id'=>$user_id,
                'token'=>$device->push_token,
                'message'=>$message,
                'os'=>$device->os,
                'os_version'=>$device->os_version,
                'phone_model'=>$device->model,
                'is_sent'=>0,
            ];

            //store notification to the DB
            $new = Notification::create($data);

        } //endforeach

    } //end method add

    /**
     * Send notifications
     */
    public function send()
    {
        //get all unsent notifications
        $notifications = Notification::where('is_sent',0)
                                        ->orderBy('id','asc')
                                        ->get();

	//dd($notifications);

        //if no notification has to be sent, stop the script
        if (!$notifications->count())
            return;

        //get push notification config file
        $configKey = "pushnotification";
        $config = config($configKey);

        //send the notification
        foreach($notifications as $device){

            //if user device OS is iOS, use the APN adapter
            if ($device->os=="iOS"){
                continue;
                //send to ios
                $pushManager = new PushManager(PushManager::ENVIRONMENT_PROD);

                // Declare an adapter.
                $apnsAdapter = new ApnsAdapter(array(
                    'certificate' => $config['apn']['certificate'],
                ));

                // Set the device(s) to push the notification to.
                $devices = new DeviceCollection(array(
                    new Device($device->token),
                ));

                // Then, create the push skel.
                $message = new Message($device->message);

                // Finally, create and add the push to the manager, and push it!
                $push = new Push($apnsAdapter, $devices, $message);
                $pushManager->add($push);
                $pushManager->push();

                $device->is_sent=1;
                $device->save();

            }
            else{
                //dd('test');
                //if user device OS is Android, use the GCM adapter
                $pushManager = new PushManager(PushManager::ENVIRONMENT_PROD);

                // Then declare an adapter.
                $gcmAdapter = new GcmAdapter(array(
                    'apiKey' => $config['gcm']['apiKey'],
                ));


                // Set the device(s) to push the notification to.
                $devices = new DeviceCollection(array(
                    new Device($device->token),
                ));

                // Then, create the push skel.
                $message = new Message($device->message);

                // Finally, create and add the push to the manager, and push it!
                try{
                    $push = new Push($gcmAdapter, $devices, $message);
                    $pushManager->add($push);
                    $pushManager->push(); // Returns a collection of notified devices

                    /*$logger = new Logger('push-notification');
                    $logger->pushHandler(new StreamHandler(public_path('files/logs/push-notif-succ.log'), Logger::INFO));
                    $logger->info('success');*/

                    $device->is_sent=1;
                    $device->save();

                }
                catch(\Exception $e){
                    dd($e->getMessage());
                    //$logger = new Logger('push-notification');
                    //$logger->pushHandler(new StreamHandler(public_path('files/logs/push-notif-err.log'), Logger::INFO));
                    //$logger->info($e->getMessage());
                    //$logger->info('error');

                }

            } //end elseif

            //mark the notification as sent



        }//end foreach


    }
}

ZeroDay Forums Mini