Example #1
0
 /**
  * Send push notification through GCM server
  *
  * @param Array $registration_ids = device_ids
  * @return result
  *
  */
 private function sendToGCM($registration_ids)
 {
     $user_notification_count = $this->getNotificationByUserTo($this->user_id_to, CONSTANTS::NOTIFICATION_STATUS_NEW, true);
     $type = $this->type;
     $activities = array();
     $user_count = 0;
     $username_array = array();
     $activity_type = 0;
     //Time Interval for notification groupings = 1 Day (86400 seconds)
     $date_range = array(KeyParser::date_from => date('Y-m-d H:i:s', time() - CONSTANTS::DAY_SECOND_VALUE), KeyParser::date_to => date('Y-m-d H:i:s'));
     //Build usernames_from array()
     if (strpos($type, 'photo')) {
         $activity_type = 5;
     } elseif (strpos($type, 'checkin')) {
         $activity_type = 2;
     } elseif (strpos($type, 'review')) {
         $activity_type = 1;
     }
     if ($type == CONSTANTS::NOTIFICATION_TYPE_LIKE_CHECKIN || $type == CONSTANTS::NOTIFICATION_TYPE_LIKE_REVIEW || $type == CONSTANTS::NOTIFICATION_TYPE_LIKE_PHOTO) {
         $activities = Like::getLikerList($activity_type, $this->type_id, false, $date_range);
     } else {
         if ($type == CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_CHECKIN || $type == CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_REVIEW || $type == CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_PHOTO) {
             $activities = Comments::getByType($activity_type, $this->type_id, $date_range);
         } else {
             $user_from = Users::find($this->user_id_from);
             $username_array[] = $user_from->getUserFullName();
         }
     }
     foreach ($activities as $activity) {
         if ($activity->user_id == $this->user_id_to) {
             continue;
         }
         $user_count++;
         $user_fullname = Users::getFullNameById($activity->user_id);
         if ($user_count <= CONSTANTS::NOTIFICATION_USER_GROUP_LIMIT && !in_array($user_fullname, $username_array)) {
             $username_array[] = $user_fullname;
         }
     }
     // Build payload
     $payload = array(KeyParser::id => $this->id, KeyParser::usernames_from => $username_array, KeyParser::count => $user_notification_count, KeyParser::type => $this->type, KeyParser::type_id => $this->type_id, KeyParser::restaurant_id => $this->restaurant_id, KeyParser::user_id_from => $this->user_id_from, KeyParser::user_id_to => $this->user_id_to, 'typeid' => '0', 'useridfrom' => '0', 'useridto' => '0', 'restaurantid' => '0', 'message' => $this->buildMessage());
     $restaurant_name = Restaurants::getRestaurantNameById($this->restaurant_id);
     if ($restaurant_name) {
         $payload[KeyParser::restaurant_name] = $restaurant_name;
     }
     $fields = array(KeyParser::registration_ids => $registration_ids, KeyParser::data => $payload);
     $headers = array('Authorization: key=' . GCM_API_KEY, 'Content-Type: application/json');
     //Prepare cURL
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, GCM_API_URL);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
     $result = curl_exec($ch);
     curl_close($ch);
     return $result;
 }