Skip to content

Instantly share code, notes, and snippets.

@joel-extremo
Created January 16, 2019 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joel-extremo/85c250114ab7ecc83f3450eb2ba9d688 to your computer and use it in GitHub Desktop.
Save joel-extremo/85c250114ab7ecc83f3450eb2ba9d688 to your computer and use it in GitHub Desktop.
NewHomeworkNotification
<?php
namespace App\Repository\PushNotification;
class NewHomeworkNotification extends OkusPushNotification
{
protected $notificationTitle = 'Nueva Tarea';
protected $notificationType = 'new_homework';
protected $messages = [
'student' => 'Tienes una nueva tarea de {{homeworkName}}.',
'tutor' => 'A {{studentName}} le han asignado una nueva tarea de {{homeworkName}}.'
];
public function send()
{
try {
$this->sendToStudents();
$this->sendToTutors();
return true;
} catch (Exception $e) {
return false;
}
}
public function sendToStudents()
{
$studentsIds = $this->homework->studentsIds();
$tokens = $this->getStudentsTokens($studentsIds);
$this->sendToDevices(
$this->getStudentMessage(),
$tokens
);
}
public function sendToTutors()
{
$students = $this->homework->section->students;
foreach ($students as $student)
{
$tutorsIds = $student->tutors()->get()->pluck('id')->toArray();
$tokens = $this->getTutorTokens($tutorsIds);
$this->sendToDevices(
$this->getTutorMessage($student->firstname),
$tokens
);
}
}
public function getTutorMessage($studentName)
{
$homeworkName = $this->homework->name;
return stringReplace(
$this->messages['tutor'],
compact('studentName', 'homeworkName')
);
}
public function getStudentMessage()
{
$homeworkName = $this->homework->name;
return stringReplace(
$this->messages['student'],
compact('homeworkName')
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment