modules/admin/controllers/CronjobController.php
/**
     * Send Notification For saved Search user For new Added Product.
     *
     *  It is execute at every minute.
     */
Send Notification For saved Search user For new Added Product.
It is execute at every minute.
Send Notification For saved Search user For new Added Product . It is execute at every minute .
public function actionSendSavedSearchNotificationForAddProduct()
    {
        $modelsProduct = DB_Product::find()->where(['is_saved_search_notification_sent' => DB_Product::IS_SAVED_SEARCH_NOTIFICATION_SENT_FALSE])->groupBy('id')->all();
        if (!empty($modelsProduct)) {
            foreach ($modelsProduct as $key => $model) {
                if (!empty($model) && $model instanceof DB_Product) {
                    $brandName = (!empty($model->brand) && !empty($model->brand->name)) ? $model->brand->name : "";
                    $categoryName = (!empty($model->category) && !empty($model->category->name)) ? $model->category->name : "";
                    $query = SearchHistory::find();
                    $query->where('user_id!=' . $model->user_id);
                    if (!empty($model->name)) {
                        $query->andFilterWhere([
                            'or',
                            ['like', 'search_text', "%" . $model->name . "%", false],
                            ['like', 'search_text', "%" . $brandName . "%", false],
                            ['like', 'search_text', "%" . $categoryName . "%", false],
                        ]);
                    }
                    $modelsSearch = $query->groupBy(['user_id', 'search_text'])->all();
                    if (!empty($modelsSearch)) {
                        foreach ($modelsSearch as $keys => $modelSearchRow) {
                            $getUsers = [];
                            if (!empty($modelSearchRow) && $modelSearchRow instanceof SearchHistory) {
                                $getUsers[] = $modelSearchRow->user;
                                if (!empty($getUsers)) {
                                    foreach ($getUsers as $keys1 => $userROW) {
                                        if ($userROW instanceof User && ($model->user_id != $userROW->id)) {
                                            if ($userROW->is_saved_searches_notification_on == User::IS_NOTIFICATION_ON && !empty($userROW->userDevice)) {
                                                $userDevice = $userROW->userDevice;
                                                if (!empty($userDevice) && !empty($userDevice->notification_token)) {
                                                    $notificationText = "Product is uploaded as per your saved search";
                                                    $modelNotification = new Notification();
                                                    $modelNotification->owner_id = $model->user_id;
                                                    $modelNotification->notification_receiver_id = $userROW->id;
                                                    $modelNotification->ref_id = $model->id;
                                                    $modelNotification->notification_text = $notificationText;
                                                    $modelNotification->action = "Add";
                                                    $modelNotification->ref_type = "products"; 
                                                    $modelNotification->save(false);
                                                    $badge = Notification::find()->where(['notification_receiver_id' => $userROW->id, 'is_read' => Notification::NOTIFICATION_IS_READ_NO])->count();
                                                    if ($userDevice->device_platform == 'android') {
                                                        $notificationToken = array($userDevice->notification_token);
                                                        $senderName = $model->user->first_name . " " . $model->user->last_name;
                                                        $notification = $modelNotification->sendPushNotificationAndroid($modelNotification->ref_id, $modelNotification->ref_type, $notificationToken, $notificationText, $senderName, $modelNotification);
                                                    } else {
                                                        $note = Yii::$app->fcm->createNotification(Yii::$app->name, $notificationText);
                                                        $note->setBadge($badge);
                                                        $note->setSound('default');
                                                        $message = Yii::$app->fcm->createMessage();
                                                        $message->addRecipient(new \paragraph1\phpFCM\Recipient\Device($userDevice->notification_token));
                                                        $message->setNotification($note)
                                                            ->setData([
                                                                'id' => $modelNotification->ref_id,
                                                                'type' => $modelNotification->ref_type,
                                                                'message' => $notificationText,
                                                                'action' => (!empty($modelNotification) && !empty($modelNotification->action)) ? $modelNotification->action : "",
                                                            ]);
                                                        $response = Yii::$app->fcm->send($message);
                                                        $result = $response->getStatusCode();
                                                    }
                                                }
                                            }
                                            if (!empty($userROW->email) && $userROW->is_saved_searches_email_notification_on == User::IS_NOTIFICATION_ON) {
                                                $message = "Product is uploaded as per your saved search.";
                                                if (!empty($userROW->email)) {
                                                    try {
                                                        Yii::$app->mailer->compose('api/addNewProductForSaveSearch', ['sender' => $model->user, 'receiver' => $userROW, 'message' => $message])
                                                            ->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->name])
                                                            ->setTo($userROW->email)
                                                            ->setSubject('New product added same as your search!')
                                                            ->send();
                                                    } catch (HttpException $e) {
                                                        echo "Error: " . $e->getMessage();
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    $model->is_saved_search_notification_sent = DB_Product::IS_SAVED_SEARCH_NOTIFICATION_SENT_TRUE;
                    $model->save(false);
                }
            }
        }
    }
public function actionSendSavedSearchNotificationForAddProduct ( ) { $ modelsProduct = DB_Product :: find ( ) -> where ( [ 'is_saved_search_notification_sent' => DB_Product :: IS_SAVED_SEARCH_NOTIFICATION_SENT_FALSE ] ) -> groupBy ( 'id' ) -> all ( ) ; if ( ! empty ( $ modelsProduct ) ) { foreach ( $ modelsProduct as $ key => $ model ) { if ( ! empty ( $ model ) && $ model instanceof DB_Product ) { $ brandName = ( ! empty ( $ model -> brand ) && ! empty ( $ model -> brand -> name ) ) ? $ model -> brand -> name : " " ; $ categoryName = ( ! empty ( $ model -> category ) && ! empty ( $ model -> category -> name ) ) ? $ model -> category -> name : " " ; $ query = SearchHistory :: find ( ) ; $ query -> where ( 'user_id!=' . $ model -> user_id ) ; if ( ! empty ( $ model -> name ) ) { $ query -> andFilterWhere ( [ 'or' , [ 'like' , 'search_text' , " % " . $ model -> name . " % " , false ] , [ 'like' , 'search_text' , " % " . $ brandName . " % " , false ] , [ 'like' , 'search_text' , " % " . $ categoryName . " % " , false ] , ] ) ; } $ modelsSearch = $ query -> groupBy ( [ 'user_id' , 'search_text' ] ) -> all ( ) ; if ( ! empty ( $ modelsSearch ) ) { foreach ( $ modelsSearch as $ keys => $ modelSearchRow ) { $ getUsers = [ ] ; if ( ! empty ( $ modelSearchRow ) && $ modelSearchRow instanceof SearchHistory ) { $ getUsers [ ] = $ modelSearchRow -> user ; if ( ! empty ( $ getUsers ) ) { foreach ( $ getUsers as $ keys1 => $ userROW ) { if ( $ userROW instanceof User && ( $ model -> user_id != $ userROW -> id ) ) { if ( $ userROW -> is_saved_searches_notification_on == User :: IS_NOTIFICATION_ON && ! empty ( $ userROW -> userDevice ) ) { $ userDevice = $ userROW -> userDevice ; if ( ! empty ( $ userDevice ) && ! empty ( $ userDevice -> notification_token ) ) { $ notificationText = " Product is uploaded as per your saved search " ; $ modelNotification = new Notification ( ) ; $ modelNotification -> owner_id = $ model -> user_id ; $ modelNotification -> notification_receiver_id = $ userROW -> id ; $ modelNotification -> ref_id = $ model -> id ; $ modelNotification -> notification_text = $ notificationText ; $ modelNotification -> action = " Add " ; $ modelNotification -> ref_type = " products " ; $ modelNotification -> save ( false ) ; $ badge = Notification :: find ( ) -> where ( [ 'notification_receiver_id' => $ userROW -> id , 'is_read' => Notification :: NOTIFICATION_IS_READ_NO ] ) -> count ( ) ; if ( $ userDevice -> device_platform == 'android' ) { $ notificationToken = array ( $ userDevice -> notification_token ) ; $ senderName = $ model -> user -> first_name . " " . $ model -> user -> last_name ; $ notification = $ modelNotification -> sendPushNotificationAndroid ( $ modelNotification -> ref_id , $ modelNotification -> ref_type , $ notificationToken , $ notificationText , $ senderName , $ modelNotification ) ; } else { $ note = Yii :: $ app -> fcm -> createNotification ( Yii :: $ app -> name , $ notificationText ) ; $ note -> setBadge ( $ badge ) ; $ note -> setSound ( 'default' ) ; $ message = Yii :: $ app -> fcm -> createMessage ( ) ; $ message -> addRecipient ( new \ paragraph1 \ phpFCM \ Recipient \ Device ( $ userDevice -> notification_token ) ) ; $ message -> setNotification ( $ note ) -> setData ( [ 'id' => $ modelNotification -> ref_id , 'type' => $ modelNotification -> ref_type , 'message' => $ notificationText , 'action' => ( ! empty ( $ modelNotification ) && ! empty ( $ modelNotification -> action ) ) ? $ modelNotification -> action : " " , ] ) ; $ response = Yii :: $ app -> fcm -> send ( $ message ) ; $ result = $ response -> getStatusCode ( ) ; } } } if ( ! empty ( $ userROW -> email ) && $ userROW -> is_saved_searches_email_notification_on == User :: IS_NOTIFICATION_ON ) { $ message = " Product is uploaded as per your saved search. " ; if ( ! empty ( $ userROW -> email ) ) { try { Yii :: $ app -> mailer -> compose ( 'api/addNewProductForSaveSearch' , [ 'sender' => $ model -> user , 'receiver' => $ userROW , 'message' => $ message ] ) -> setFrom ( [ Yii :: $ app -> params [ 'adminEmail' ] => Yii :: $ app -> name ] ) -> setTo ( $ userROW -> email ) -> setSubject ( 'New product added same as your search!' ) -> send ( ) ; } catch ( HttpException $ e ) { echo " Error:  " . $ e -> getMessage ( ) ; } } } } } } } } } $ model -> is_saved_search_notification_sent = DB_Product :: IS_SAVED_SEARCH_NOTIFICATION_SENT_TRUE ; $ model -> save ( false ) ; } } } }
Send Notification For saved Search user For new Added Product.
Send Notification For saved Search user For new Added Product .
// Send Push Notification and Email notification start // Insert into notification. // For add new product // \Yii::info("\n------------android notification ----------------\n" . "userId:" . $userROW->id . "\n" . $notification, 'notifyUserBasedOnsaveSearch'); // \Yii::info("\n------------ios notification ----------------\n" . "userId:" . $userROW->id . "\n" . $result, 'notifyUserBasedOnsaveSearch'); // Send Push Notification and Email notification end //die("Notification sent. \n");