where('is_active', 1) ->orderByDesc('id') ->get(); $currentDateTime = new DateTime(); foreach ($getAllPushNotifications as $inAppNotificationItem) { $storedDateTime = new DateTime($inAppNotificationItem->delivery_schedule); $currentTime = $currentDateTime->format('Y-m-d H:i'); $storedTime = $storedDateTime->format('Y-m-d H:i'); if ($currentTime == $storedTime) { $title = $inAppNotificationItem->type; $description = $inAppNotificationItem->description; $imagePath = $inAppNotificationItem->image; $stateIds = json_decode($inAppNotificationItem->state_ids, true); $userType = $inAppNotificationItem->user_type; if ($userType == 1) { // Subscribed users $iamPrincipals = Subscriptions::select('iam_principal_xid') ->where('next_payment_date', '>=', now()) ->get(); $iamPrincipalIds = $iamPrincipals->pluck('iam_principal_xid'); $users = IamPrincipal::whereIn('id', $iamPrincipalIds) ->where('is_active', 1) ->where('notification_status', 1) ->where('principal_type_xid', 3) ->whereIn('state_xid', $stateIds) ->get(); } elseif ($userType == 2) { // Unsubscribed users $allPrincipalIds = IamPrincipal::where('principal_type_xid', 3)->pluck('id'); $subscribedIds = Subscriptions::select('iam_principal_xid') ->where('next_payment_date', '>=', now()) ->pluck('iam_principal_xid'); $unsubscribedIds = $allPrincipalIds->diff($subscribedIds); $users = IamPrincipal::whereIn('id', $unsubscribedIds) ->where('is_active', 1) ->where('notification_status', 1) ->whereIn('state_xid', $stateIds) ->get(); } elseif ($userType == 3) { // Both subscribed and unsubscribed users $users = IamPrincipal::where('is_active', 1) ->where('notification_status', 1) ->where('principal_type_xid', 3) ->whereIn('state_xid', $stateIds) ->get(); } foreach ($users as $user) { if ($user->one_signal_player_id) { OneSignalHelper::sendNotificationApi( $user->one_signal_player_id, $title, $description, 'Dashboard Notification', $imagePath, $id = null ); Log::info("INAPP scheduled notification sent successfully to user ID: {$user->id}"); onesignalhelper::StoreNotificationDetails($user->id, 'Notification', $title, $imagePath); $inAppNotificationItem->is_active = 0; $inAppNotificationItem->save(); } } } } } }