public function sendNotification($message_id, $message, $users)
 {
     $this->init();
     try {
         foreach ($users as $user) {
             $user_id = $user->id;
             $registatoin_ids = array($user->gcm_regid);
             $json = GCM::get()->send_notification($registatoin_ids, $message);
             if (!$json) {
                 return -400;
                 //Curl failed
             }
             $ret = json_decode($json, true);
             if ($ret['success']) {
                 $state = 1;
                 //success
                 $this->success_count++;
                 $state_text = $ret['results'][0]['message_id'];
             } elseif ($ret['failure']) {
                 $state = 2;
                 //failed
                 $this->fail_count++;
                 $state_text = $ret['results'][0]['error'];
             } else {
                 $state = 2;
                 //failed
                 $this->fail_count++;
                 $state_text = strpos($json, 'Unauthorized') ? 'Unauthorized API key' : 'Undefined';
             }
             //                $state_text = isset($ret['results'][0]['error']) ? $ret['results'][0]['error'] : $ret['results'][0]['message_id'];
             $message_state = new MessageState();
             $message_state->message_id = $message_id;
             $message_state->user_id = $user_id;
             $message_state->state = $state;
             $message_state->state_text = $state_text;
             $message_state->save();
         }
         return 101;
     } catch (Exception $e) {
         return -100;
     }
 }
Beispiel #2
0
 public function findMessages($state = false)
 {
     $user_id = $this->id;
     $unreadMessages = Message::whereHas('messageStates', function ($q) use(&$user_id, &$state) {
         $q->where('user_id', '=', $user_id);
         if ($state) {
             $q->where('state', '=', MessageState::indexOf($state));
         }
     })->with('conversation')->get();
     return $unreadMessages;
 }