How to include external class file for writting ScheduleTask

How to include external class file for writting ScheduleTask

by Baiju Sharma -
Number of replies: 2

Hi,

I am writing a moodle schedule task which will run automatically using Schedule task.I need to send a push notification to iOS mobile when this scheduled task will run.

For this, I have imported PushNotifications.php class external file into my working folder i.e "/home/onlin200/public_html/mod/certificate/classes/task/PushNotifications.php"  which will help me send notification. 

The file in which I am writing my schedule task is also in the same folder "/home/onlin200/public_html/mod/certificate/classes/task/drill_re_certification_notification.php"

The class file which I am importing: https://gist.github.com/joashp/b2f6c7e24127f2798eb2 into my task folder.I want to access function written in PushNotifications.php class file into my Task file.   


Below is my code.

<?php


namespace mod_certificate\task;

require('PushNotifications.php');

class drill_re_certification_notification extends \core\task\scheduled_task{

   function execute() {

        

         global $DB;

         $subject = get_string('recertify', 'mod_certificate','');

         $emailbody = get_string('recertify_emailbody', 'mod_certificate','');

         $msg_payload = array (

'mtitle' => 'Test push notification title',

'mdesc' => 'Test push notification body',

    );      

          $DATA = $DB->get_recordset_sql ("Select notifydate,deviceToken from vw_user_recertification_notify");        

foreach ($DATA as $id => $student) {   

                  $record = new \stdClass();                               

                    $record ->email = $student->email;

                    $record ->notifydate= $student->notifydate;

                    $record ->devicetoken= $student->devicetoken; 

                    if(time() == time())

                    {

                        PushNotifications::iOS($msg_payload, (string)($record ->deviceToken));  --Function                   

                         if (mail((string)($record ->email), $subject, $emailbody,  $header, '-f'."b@test.com"))

                         {                            

                         }

                       

    }

}

}

Error message:


Fatal error: Class 'mod_certificate\task\PushNotifications' not found in /home/onlin200/public_html/mod/certificate/classes/task/drill_re_certification_notification.php on line 61


Average of ratings: -
In reply to Baiju Sharma

Re: How to include external class file for writting ScheduleTask

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
\PushNotifications::iOS($msg_payload, (string)($record->deviceToken));
Average of ratings: Useful (1)
In reply to Renaat Debleu

Re: How to include external class file for writting ScheduleTask

by Baiju Sharma -

Thank you, It was really helpful.

But I ran into different errors,If possible could you Please check.


Deprecated: Non-static method PushNotifications::iOS() should not be called statically, assuming $this from incompatible context in /home/onlin200/public_html/mod/certificate/classes/task/drill_re_certification_notification.php on line 64

Notice: Undefined property: stdClass::$deviceToken in /home/onlin200/public_html/mod/certificate/classes/task/drill_re_certification_notification.php on line 64

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /home/onlin200/public_html/mod/certificate/classes/task/pushnotifications.php on line 85

Warning: stream_socket_client(): Failed to enable crypto in /home/onlin200/public_html/mod/certificate/classes/task/pushnotifications.php on line 85

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/onlin200/public_html/mod/certificate/classes/task/pushnotifications.php on line 85
Failed to connect: 0