/** * Creates a new PushNotifications model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { set_time_limit(0); $model = new PushNotifications(); $isModelLoaded = $model->load(Yii::$app->request->post()); if ($isModelLoaded) { $deviceTokenModel = new \backend\models\UserDeviceTokens(); $deviceTokenModels = $deviceTokenModel->getActiveDeviceTokens(); foreach ($deviceTokenModels as $deviceTokenModel) { $notificationCount = $deviceTokenModel->notification_count; if (strlen($deviceTokenModel->device_token) > 3) { $apnsGcm = Yii::$app->apnsGcm; $apnsGcm->send(\bryglen\apnsgcm\ApnsGcm::TYPE_APNS, $deviceTokenModel->device_token, $model->message, ['customerProperty' => 1], ['sound' => 'default', 'badge' => $notificationCount++]); $deviceTokenModel->notification_count = $notificationCount; $deviceTokenModel->save(); } } Yii::$app->getSession()->setFlash('success', 'Notification sent!'); return $this->redirect(['site/index']); } else { return $this->render('create', ['model' => $model]); } }
/** * Sets count of notification in iPad * @return mixed */ public function actionSetCount() { $request = yii::$app->request; $userId = $request->post('user_id', false); $deviceToken = $request->post('device_token', false); $notificationCount = (int) $request->post('count', false); if ($userId && $deviceToken && $notificationCount >= 0) { $deviceTokenModel = new \backend\models\UserDeviceTokens(); $row = $deviceTokenModel->find()->where(['user_id' => $userId, 'device_token' => $deviceToken])->one(); if ($row) { try { $row->notification_count = $notificationCount; $row->save(); return ['message' => 'success']; } catch (\Exception $ex) { return ['message' => 'Error occured', 'detail' => $ex->getMessage()]; } } else { return ['message' => 'Unable to find such data']; } } else { return ['message' => 'Invalid Data']; } }