File "push.inc.php"

Full Path: /srv/www/www.cadoro.it/src/controllers/push.inc.php
File size: 1.97 KB
MIME-type: text/x-php
Charset: utf-8

<?php
  header("Content-Type: text/plain; charset=\"UTF-8\"");

  $notifications = models\Notification::get(array("push" => 1));
  foreach ($notifications as $notification) {
    $status = 0;
    foreach (models\User::get(array("q" => $notification['target_filter'], "newsletter" => 1), 0, 100) as $user) {
      // android push notification
      if ($user['token_type'] == 'android') {
        $data = array();
        $data['title'] = $notification['title'];
        $data['message'] = $notification['message'];
        $data['style'] = "inbox";
        $data['summaryText'] = "Hai ricevuto %n% notifiche";
        $data['vibrate'] = 1;
        $data['sound'] = 1;
        $data['largeIcon'] = "large_icon";
        $data['smallIcon'] = "small_icon";
        $n_data = json_decode($notification['data'], true);
        if ($n_data) {
          foreach ($n_data as $k => $v) {
            $data[$k] = $v;
          }
        }
        $data['notification_id'] = (int)$notification['id'];
        classes\Push::android($user['token'], $data);
        $status++;
      }
      // ios push notification
      else if ($user['token_type'] == 'ios') {
        $data = array_filter(array(
          'aps' => array(
            'badge'           => 0,
            'clearBadge'      => 1,
            'alert'           => $notification['title'],
            'sound'           => 'default',
          ),
        ));
        $n_data = json_decode($notification['data'], true);
        if ($n_data) {
          foreach ($n_data as $k => $v) {
            $data[$k] = $v;
          }
        }
        $data['notification_id'] = (int)$notification['id'];
        classes\Push::ios($user['token'], $data);
        $status++;
      }
    }
    echo "NOTIFICATION #" . $notification['id'] . " = " . $status . " messages\n";
    $notification->update_push(array(
      "target_filter" => $notification['target_filter'],
      "target_datetime" => _ddt($notification['target_datetime']),
      "target_status" => 2,
    ));
  }